#define mm(a) memset(a,0,sizeof(a));
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)<(y)?(x):(y)
#define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN = 2e5+5;

int x,n,m,cnta,cntb,w[MAXN];
struct node {
    int val,id;
} a[MAXN],b[MAXN];
struct tree {
    int x,y;
}ans[MAXN];

bool cmp(node a,node b) {
    return a.val<b.val;
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++) {
        int val,is;
        scanf("%d%d",&val,&is);
        if(is==1) {
            cnta++;
            a[cnta].val=val;
            a[cnta].id=i;
        } else {
            cntb++;
            b[cntb].val=val;
            b[cntb].id=i;
        }
    }
    sort(a+1,a+1+cnta,cmp);
    sort(b+1,b+1+cntb,cmp);
    int now=1;
    for(int i=1;i<=cnta;i++){
        w[now]=a[i].val;
        ans[a[i].id].x=1;
        ans[a[i].id].y=++now;
    }
    int pos=3;
    now=2;
    for(int i=1;i<=cntb;i++){
        if(w[pos]>b[i].val){
            printf("-1\n");
            return 0;
        }
        ans[b[i].id].x=pos;
        ans[b[i].id].y=now;
        now++;
        if(now==pos)pos++,now=2;
    }
    for(int i=1;i<=m;i++)printf("%d %d\n",ans[i].x,ans[i].y);
    return 0;
}


#define mm(a) memset(a,0,sizeof(a));
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)<(y)?(x):(y)
#define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN = 2e5+5;

int x,n,m,cnta,cntb,w[MAXN];
struct node {
    int val,id;
} a[MAXN],b[MAXN];
struct tree {
    int x,y;
} ans[MAXN];

bool cmp(node a,node b) {
    return a.val<b.val;
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++) {
        int val,is;
        scanf("%d%d",&val,&is);
        if(is==1) {
            cnta++;
            a[cnta].val=val;
            a[cnta].id=i;
        } else {
            cntb++;
            b[cntb].val=val;
            b[cntb].id=i;
        }
    }
    sort(a+1,a+1+cnta,cmp);
    sort(b+1,b+1+cntb,cmp);
    int now=1;
    for(int i=1; i<=cnta; i++) {
        ans[a[i].id].x=1;
        ans[a[i].id].y=++now;
        w[now]=a[i].val;
    }
    int pos=3;
    now=2;
    for(int i=1; i<=cntb; i++) {
        if(w[pos]>b[i].val)return printf("-1\n"),0;
        ans[b[i].id].x=pos;
        ans[b[i].id].y=now;
        now++;
        if(now==pos)pos++,now=2;
    }
    for(int i=1; i<=m; i++)printf("%d %d\n",ans[i].x,ans[i].y);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector <pair <int, pair <int, int> > > vec;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        b = !b;

        vec.push_back({a, {b, i}});
    }

    sort(vec.begin(), vec.end());

    vector <pair <int, pair <int, int> > > ans;
    int cur = 1, all = 1, r = 1;

    for (int i = 0; i < m; i++) {
        if (vec[i].second.first == 0) {
            all++;
            ans.push_back({vec[i].second.second, {1, all}});
            continue;
        }

        if (r == cur) {
            cur++;
            r = 2;
            if (r == cur) {
                cur++;
            }
        }
        if (cur > all) {
            cout << -1;
            return 0;
        }

        ans.push_back({vec[i].second.second, {cur, r}});

        r++;
    }

    if (all != n) {
        cout << -1;
        return 0;
    }

    sort(ans.begin(), ans.end());

    for (int i = 0; i < m; i++) {
        cout << ans[i].second.first << " " << ans[i].second.second << '\n';
    }

    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector <pair <int, int> > vec;

int main()
{
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        b = !b;

        vec.push_back({a, b});
    }

    sort(vec.begin(), vec.end());

    vector <pair <int, int> > ans;
    int cur = 1, all = 1, r = 1;

    for (int i = 0; i < m; i++) {
        if (vec[i].second == 0) {
            all++;
            ans.push_back({1, all});
            continue;
        }

        if (r == cur) {
            cur++;
            r = 2;
            if (r == cur) {
                cur++;
            }
        }
        if (cur > all) {
            cout << -1;
            return 0;
        }

        ans.push_back({cur, r});

        r++;
    }

    if (all != n) {
        cout << -1;
        return 0;
    }

    for (int i = 0; i < m; i++) {
        cout << ans[i].first << " " << ans[i].second << '\n';
    }

    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector <pair <int, pair <int, int> > > vec;

int main()
{
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        b = !b;

        vec.push_back({a, {b, i}});
    }

    sort(vec.begin(), vec.end());

    vector <pair <int, pair <int, int> > > ans;
    int cur = 1, all = 1, r = 1;

    for (int i = 0; i < m; i++) {
        if (vec[i].second.first == 0) {
            all++;
            ans.push_back({vec[i].second.second, {1, all}});
            continue;
        }

        if (r == cur) {
            cur++;
            r = 2;
            if (r == cur) {
                cur++;
            }
        }
        if (cur > all) {
            cout << -1;
            return 0;
        }

        ans.push_back({vec[i].second.second, {cur, r}});

        r++;
    }

    if (all != n) {
        cout << -1;
        return 0;
    }

    sort(ans.begin(), ans.end());

    for (int i = 0; i < m; i++) {
        cout << ans[i].second.first << " " << ans[i].second.second << '\n';
    }

    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector<pair<int,int>> inside;
vector<pair<int,int>> outside;
vector<pair<int, pair<int,int>>> ans;

int main()
{
    int n, m ,w, b;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> w >> b;
        if (b == 0)
        {
            outside.push_back({w,i});
        }
        if (b == 1)
        {
            inside.push_back({w,i});
        }
    }
    sort(outside.begin(), outside.end());
    sort(inside.begin(), inside.end());
    int tmp = 1;
    int f = 0;
    for (int i = 0 ; i < inside.size(); i++)
    {
        tmp++;
        ans.push_back({inside[i].second, {1, tmp}});
        if (f < outside.size())
        {

            for (int j = 0; j < i; j++)
            {
                if(f == outside.size())
                {
                    break;
                }
                if (outside[f].first < inside[i].first)
                {
                    cout << -1;
                    return 0;
                }
                else
                {
                    ans.push_back({outside[f].second,{tmp, j + 2}});
                    f++;
                }
            }
        }

    }
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i].second.first << " " << ans[i].second.second << endl;
    }

}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpll vector<pll>
#define vpii vector<pii>
#define umap unordered_map
#define uset unordered_set
#define PQ priority_queue

#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?'\n':' ')
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?'\n':' ')
#define pb push_back
#define eb emplace_back
#define mt make_tuple
#define fbo find_by_order
#define ook order_of_key
#define MP make_pair
#define UB upper_bound
#define LB lower_bound
#define SQ(x) ((x)*(x))
#define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
#define F first
#define S second
#define mem(a,x) memset(a,x,sizeof(a))
#define E 2.71828182845904523536
#define gamma 0.5772156649
#define nl "\n"
#define lg(r,n) (int)(log2(n)/log2(r))
#define sz(v) (int)v.size()
#define pf printf
#define sf scanf
#define sf1(a)                scanf("%d",&a)
#define sf2(a,b)              scanf("%d %d",&a,&b)
#define sf3(a,b,c)            scanf("%d %d %d",&a,&b,&c)
#define pf1(a)                printf("%d\n",a);
#define pf2(a,b)              printf("%d %d\n",a,b)
#define pf3(a,b,c)            printf("%d %d %d\n",a,b,c)
#define sf1ll(a)              scanf("%lld",&a)
#define sf2ll(a,b)            scanf("%I64d %I64d",&a,&b)
#define sf3ll(a,b,c)          scanf("%I64d %I64d %I64d",&a,&b,&c)
#define pf1ll(a)              printf("%lld\n",a);
#define pf2ll(a,b)            printf("%I64d %I64d\n",a,b)
#define pf3ll(a,b,c)          printf("%I64d %I64d %I64d\n",a,b,c)
#define _ccase printf("Case %lld: ",++cs)
#define _case cout<<"Case "<<++cs<<": "
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; }

#define asche cerr<<"Ekhane asche\n";
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
#define all(v) v.begin(),v.end()
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define toint(a) atoi(a.c_str())
#define BeatMeScanf ios_base::sync_with_stdio(false)
#define valid(tx,ty) (tx>=0&&tx<n&&ty>=0&&ty<m)
#define one(x) __builtin_popcount(x)
#define Unique(v) v.erase(unique(all(v)),v.end())
#define stree l=(n<<1),r=l+1,mid=b+(e-b)/2
#define fout(x) fixed<<setprecision(x)
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}
inline void yes(){cout<<"YES\n";exit(0);}
inline void no(){cout<<"NO\n";exit(0);}
template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll dx[]={1,0,-1,0,1,-1,-1,1};
ll dy[]={0,1,0,-1,1,1,-1,-1};
//random_device rd;
//mt19937 rnd(rd());
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {}
template<typename T, typename... Args>
void deb(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    deb(++it, args...);
}

const int mod=1e9+7;
const int N=3e5+9;
const ld eps=1e-9;
const ld PI=acos(-1.0);
ll gc(ll a,ll b){while(b){ll x=a%b;a=b;b=x;}return a;}
ll lc(ll a,ll b){return a/gc(a,b)*b;}
ll qpow(ll n,ll k) {ll ans=1;assert(k>=0);n%=mod;while(k>0){if(k&1) ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans%mod;}

pii ans[N];
int w[N],b[N],ord[N];
bool cmp(int i,int j)
{
    if(w[i]==w[j]) return b[i]>b[j];
    else return w[i]<w[j];
}
int32_t main()
{
    BeatMeScanf;
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>w[i]>>b[i],ord[i]=i;
    vpii ed;
    for(i=1;i<=n&&sz(ed)<=m;i++){
        for(j=i-2;j>=1&&sz(ed)<=m;j--){
            ed.eb(i,j);
        }
    }
    sort(ord+1,ord+m+1,cmp);
    int idx=0;
    int nw=1;
    for(i=1;i<=m;i++){
        k=ord[i];
        if(b[k]==1){
            ans[k]={nw,nw+1};
            nw++;
        }
        else{
            if(ed[idx].F>nw) return cout<<-1<<nl,0;
            ans[k]=ed[idx];
        }
    }
    for(i=1;i<=m;i++) cout<<ans[i].F<<' '<<ans[i].S<<nl;
    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpll vector<pll>
#define vpii vector<pii>
#define umap unordered_map
#define uset unordered_set
#define PQ priority_queue

#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?'\n':' ')
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?'\n':' ')
#define pb push_back
#define eb emplace_back
#define mt make_tuple
#define fbo find_by_order
#define ook order_of_key
#define MP make_pair
#define UB upper_bound
#define LB lower_bound
#define SQ(x) ((x)*(x))
#define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
#define F first
#define S second
#define mem(a,x) memset(a,x,sizeof(a))
#define E 2.71828182845904523536
#define gamma 0.5772156649
#define nl "\n"
#define lg(r,n) (int)(log2(n)/log2(r))
#define sz(v) (int)v.size()
#define pf printf
#define sf scanf
#define sf1(a)                scanf("%d",&a)
#define sf2(a,b)              scanf("%d %d",&a,&b)
#define sf3(a,b,c)            scanf("%d %d %d",&a,&b,&c)
#define pf1(a)                printf("%d\n",a);
#define pf2(a,b)              printf("%d %d\n",a,b)
#define pf3(a,b,c)            printf("%d %d %d\n",a,b,c)
#define sf1ll(a)              scanf("%lld",&a)
#define sf2ll(a,b)            scanf("%I64d %I64d",&a,&b)
#define sf3ll(a,b,c)          scanf("%I64d %I64d %I64d",&a,&b,&c)
#define pf1ll(a)              printf("%lld\n",a);
#define pf2ll(a,b)            printf("%I64d %I64d\n",a,b)
#define pf3ll(a,b,c)          printf("%I64d %I64d %I64d\n",a,b,c)
#define _ccase printf("Case %lld: ",++cs)
#define _case cout<<"Case "<<++cs<<": "
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; }

#define asche cerr<<"Ekhane asche\n";
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
#define all(v) v.begin(),v.end()
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define toint(a) atoi(a.c_str())
#define BeatMeScanf ios_base::sync_with_stdio(false)
#define valid(tx,ty) (tx>=0&&tx<n&&ty>=0&&ty<m)
#define one(x) __builtin_popcount(x)
#define Unique(v) v.erase(unique(all(v)),v.end())
#define stree l=(n<<1),r=l+1,mid=b+(e-b)/2
#define fout(x) fixed<<setprecision(x)
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}
inline void yes(){cout<<"YES\n";exit(0);}
inline void no(){cout<<"NO\n";exit(0);}
template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll dx[]={1,0,-1,0,1,-1,-1,1};
ll dy[]={0,1,0,-1,1,1,-1,-1};
//random_device rd;
//mt19937 rnd(rd());
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {}
template<typename T, typename... Args>
void deb(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    deb(++it, args...);
}

const int mod=1e9+7;
const int N=3e5+9;
const ld eps=1e-9;
const ld PI=acos(-1.0);
ll gc(ll a,ll b){while(b){ll x=a%b;a=b;b=x;}return a;}
ll lc(ll a,ll b){return a/gc(a,b)*b;}
ll qpow(ll n,ll k) {ll ans=1;assert(k>=0);n%=mod;while(k>0){if(k&1) ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans%mod;}

pii ans[N];
int w[N],b[N],ord[N];
bool cmp(int i,int j)
{
    if(w[i]==w[j]) return b[i]>b[j];
    else return w[i]<w[j];
}
int32_t main()
{
    BeatMeScanf;
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>w[i]>>b[i],ord[i]=i;
    vpii ed;
    for(i=1;i<=n&&sz(ed)<=m;i++){
        for(j=i-2;j>=1&&sz(ed)<=m;j--){
            ed.eb(i,j);
        }
    }
    sort(ord+1,ord+m+1,cmp);
    int idx=0;
    int nw=1;
    for(i=1;i<=m;i++){
        k=ord[i];
        if(b[k]==1){
            ans[k]={nw,nw+1};
            nw++;
        }
        else{
            if(ed[idx].F>nw) return cout<<-1<<nl,0;
            ans[k]=ed[idx];
    idx++;
        }
    }
    for(i=1;i<=m;i++) cout<<ans[i].F<<' '<<ans[i].S<<nl;
    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


#include<bits/stdc++.h>
using namespace std;
struct node
{
    int ind,wei;
    bool operator < (const node & p)const
    {
        return wei<p.wei;
    }
};
struct edge
{
    int u,v,w;
    bool operator < (const edge &p)const
    {
        return w<p.w;
    }
};
vector<node>ms,mt;
vector<edge>vc;
int par[300009],ans[300009],ans2[300009];
int find(int v)
{
    if(par[v]==v)return v;
    else return par[v]=find(par[v]);
}
long long mst(int n)
{
    int p,q,x,y;
    for(int i=0;i<=n;i++)par[i]=i;
    long long s=0;
    int cnt=0;
    int l=vc.size();
    for(int i=0;i<vc.size();i++)
    {
        p=find(vc[i].u);
        q=find(vc[i].v);
        if(p!=q)
        {
            s+=vc[i].w;
            cnt++;
            par[q]=p;
            if(cnt==n-1)break;
        }
    }
    return s;
}
main()
{  long long sum=0;
  int m,n,k,v;
    scanf("%d%d",&n,&v);
    for(int i=1;i<=v;i++)
    {
        scanf("%d%d",&m,&k);
        node gr;
        if(k==1)
        {
            gr.ind=i;
            gr.wei=m;
            ms.push_back(gr);
        }
        else if(k==0)
        {
            gr.ind=i;
            gr.wei=m;
            mt.push_back(gr);
        }
    }
    sort(ms.begin(),ms.end());
    sort(mt.begin(),mt.end());
    int j=1;
    for(int i=0;i<ms.size();i++)
    {
        node gr=ms[i];
        ans[gr.ind]=j;
        ans2[gr.ind]=j+1;
        edge pr;
        pr.u=j;
        pr.v=j+1;
        pr.w=gr.wei;
        sum+=gr.wei;
        j++;
        vc.push_back(pr);
    }
    int cnt=1;
    j=3;
    for(int i=0;i<mt.size();i++)
    {
        node gr=mt[i];
        ans[gr.ind]=cnt;
        ans2[gr.ind]=j;
        if(j>n)
        {
            printf("-1\n");
            return 0;
        }
        edge pr;
        pr.u=cnt;
        pr.v=j;
        pr.w=gr.wei;
        vc.push_back(pr);
        cnt++;
        if(cnt==j-1)
        {
            cnt=1;
            j++;
        }
    }
    sort(vc.begin(),vc.end());
    if(sum==mst(n))
    {
        for(int i=1;i<=v;i++)
        {
            printf("%d %d\n",ans[i],ans2[i]);
        }
        return 0;
    }
    printf("-1\n");
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;

    cin >> n >> m;
    vector<tuple<int, int, int, int, int>> edges(m);
    for (int i = 0; i < m; i++) {
        auto& [w, b, id, u, v] = edges[i];
        cin >> w >> b;
        id = i;
        u = v = -1;
    }

    sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
        return (get<0>(a) < get<0>(b)) || (get<0>(a) == get<0>(b) && get<1>(a) > get<1>(b));
    });

    int main_size = 1;
    int from = 2, to = 3;
    bool ok = true;

    for (auto& [w, b, id, u, v]: edges) {
        if (b == 1) {
            u = 1;
            v = ++main_size;
        }
        else {
            u = from;
            v = to;
            if (to > main_size) {
                ok = false;
                break;
            }
            if (++from == to) {
                from = 2;
                ++to;
            }
        }
    }

    if (ok) {
        sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
            return get<2>(a) < get<2>(b);
        });

        for (const auto& [w, b, id, u, v]: edges)
            cout << u << " " << v << endl;
    }
    else
        cout << "-1" << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;

    cin >> n >> m;
    vector<tuple<int, int, int, int, int>> edges(m);
    for (int i = 0; i < m; i++) {
        auto& [w, b, id, u, v] = edges[i];
        cin >> w >> b;
        id = i;
        u = v = -1;
    }

    sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
        return (get<0>(a) < get<0>(b)) || (get<0>(a) == get<0>(b) && get<1>(a) > get<1>(b));
    });

    set<pair<int, int>> s;
    vector<int> deg(n+1);
    int main_size = 1;
    bool ok = true;
    s.insert(pair(0, 1));

    for (auto& [w, b, id, u, v]: edges) {
        if (b == 1) {
            u = 1;
            v = ++main_size;
            s.erase(pair(deg[u], u));
            s.insert(pair(++deg[u], u));
            s.insert(pair(++deg[v], v));
        }
        else {
            if (s.size() < 2) {
                ok = false;
                break;
            }
            auto [du, eu] = *s.begin();
            s.erase(pair(du, eu));
            auto [dv, ev] = *s.begin();
            s.erase(pair(dv, ev));
            if (du >= main_size-1 || dv >= main_size-1) {
                ok = false;
                break;
            }
            ++deg[u=eu];
            ++deg[v=ev];
            s.insert(pair(deg[u], u));
            s.insert(pair(deg[v], v));
        }
    }

    if (ok) {
        sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
            return get<2>(a) < get<2>(b);
        });

        for (const auto& [w, b, id, u, v]: edges)
            cout << u << " " << v << endl;
    }
    else
        cout << "-1" << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;

    cin >> n >> m;
    vector<tuple<int, int, int, int, int>> edges(m);
    for (int i = 0; i < m; i++) {
        auto& [w, b, id, u, v] = edges[i];
        cin >> w >> b;
        id = i;
        u = v = -1;
    }

    sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
        return (get<0>(a) < get<0>(b)) || (get<0>(a) == get<0>(b) && get<1>(a) > get<1>(b));
    });

    set<pair<int, int>> s;
    vector<int> deg(n+1);
    int main_size = 1;
    bool ok = true;
    s.insert(pair(0, 1));

    for (auto& [w, b, id, u, v]: edges) {
        if (b == 1) {
            u = 1;
            v = ++main_size;
            s.erase(pair(deg[u], u));
            s.insert(pair(++deg[u], u));
            s.insert(pair(++deg[v], v));
        }
        else {
            if (s.size() < 2) {
                ok = false;
                break;
            }
            auto [du, eu] = *s.begin();
            s.erase(pair(du, eu));
            auto [dv, ev] = *s.begin();
            s.erase(pair(dv, ev));
            if (du >= main_size || dv >= main_size) {
                ok = false;
                break;
            }
            ++deg[u=eu];
            ++deg[v=ev];
            s.insert(pair(deg[u], u));
            s.insert(pair(deg[v], v));
        }
    }

    if (ok) {
        sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
            return get<2>(a) < get<2>(b);
        });

        for (const auto& [w, b, id, u, v]: edges)
            cout << u << " " << v << endl;
    }
    else
        cout << "-1" << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;

    cin >> n >> m;
    vector<tuple<int, int, int, int, int>> edges(m);
    for (int i = 0; i < m; i++) {
        auto& [w, b, id, u, v] = edges[i];
        cin >> w >> b;
        id = i;
        u = v = -1;
    }

    sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
        return (get<0>(a) < get<0>(b)) || (get<0>(a) == get<0>(b) && get<1>(a) > get<1>(b));
    });

    set<pair<int, int>> s;
    vector<int> deg(n+1);
    int main_size = 1;
    bool ok = true;
    s.insert(pair(0, 1));

    for (auto& [w, b, id, u, v]: edges) {
        if (b == 1) {
            u = main_size;
            v = ++main_size;
            s.erase(pair(deg[u], u));
            s.insert(pair(++deg[u], u));
            s.insert(pair(++deg[v], v));
        }
        else {
            if (s.size() < 2) {
                ok = false;
                break;
            }
            auto [du, eu] = *s.begin();
            int ev = du + 2;
            int dv = deg[ev];
            if (ev > main_size) {
                ok = false;
                break;
            }
            s.erase(pair(du, eu));
            s.erase(pair(dv, ev));
            ++deg[u=eu];
            ++deg[v=ev];
            s.insert(pair(deg[u], u));
            s.insert(pair(deg[v], v));
        }
    }

    if (ok) {
        sort(begin(edges), end(edges), [](const auto& a, const auto& b) {
            return get<2>(a) < get<2>(b);
        });

        for (const auto& [w, b, id, u, v]: edges)
            cout << u << " " << v << endl;
    }
    else
        cout << "-1" << endl;

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 100005

struct node
{
    int x,y,z;
};
bool cmp(node A,node B)
{
    if(A.x==B.x)
        return A.y>B.y;
    return A.x<B.x;
}
int l = 2,r = 2;
vector<node> p;
int ans1[maxn],ans2[maxn];
int vis[maxn];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        node k;
        scanf("%d%d",&k.x,&k.y);
        k.z = i;
        p.push_back(k);
    }
    sort(p.begin(),p.end(),cmp);
    int rr = 2;
    for(int i=0;i<p.size();i++)
    {
        if(p[i].y==1)
        {
            ans1[p[i].z]=1,ans2[p[i].z]=rr;
            vis[rr]=1;rr++;
        }
        else if(p[i].y==0)
        {
            if(l==r)
            {
                r++;l=2;
                if(vis[r]==0)return puts("-1");
            }
            ans1[p[i].z]=l++,ans2[p[i].z]=r;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d %d\n",ans1[i],ans2[i]);
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c;
vector<int>a,b;
vector<pair<int,int>>ans;

int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back(x[i]);
    else
        b.push_back(x[i]);
}
sort(a.rbegin(),a.rend());
sort(b.begin(),b.end());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1]>=a[i]&&b[b.size()-1]>=a[j])
            ans.push_back({n-i,n-j}),b.pop_back();
            else
            {
                cout<<"-1";
                return 0;
            }
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<++c<<endl;
    else
        cout<<ans[ans.size()-1].F<<" "<<ans[ans.size()-1].S<<endl,ans.pop_back();
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<int>b;
vector<pair<int,int>>ans,a;

int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({x[i],++c});
    else
        b.push_back(x[i]);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1]>=a[i].F&&b[b.size()-1]>=a[j].F)
            ans.push_back({a[i].S,a[j].S}),b.pop_back();
            else
            {
                cout<<"-1";
                return 0;
            }
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
c=0;
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<a[c++].F<<endl;
    else
        cout<<ans[ans.size()-1].F<<" "<<ans[ans.size()-1].S<<endl,ans.pop_back();
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<int>b;
vector<pair<int,int>>ans,a;

int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({x[i],++c});
    else
        b.push_back(x[i]);
}
sort(a.begin(),a.end());
sort(b.rbegin(),b.rend());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1]>=a[i].F&&b[b.size()-1]>=a[j].F)
            ans.push_back({a[i].S,a[j].S}),b.pop_back();
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
if(b.size())
{
    cout<<"-1";
    return 0;
}
c=0;
reverse(ans.begin(),ans.end());
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<a[c++].S<<endl;
    else
        cout<<ans[ans.size()-1].F<<" "<<ans[ans.size()-1].S<<endl,ans.pop_back();
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<pair<int,int>>a,b;
pair<int,int>ans[100010];
int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({x[i],++c});
    else
        b.push_back({x[i],i});
}
sort(a.begin(),a.end());
sort(b.rbegin(),b.rend());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1].F>=a[i].F)
            ans[b[b.size()-1].S]={a[i].S,a[j].S},b.pop_back();
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
if(b.size())
{
    cout<<"-1";
    return 0;
}
c=1;
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<++c<<endl;
    else
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<pair<int,int>>b;
vector<pair<pair<int,int>,int>>a;
pair<int,int>ans[100010];
int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({{x[i],++c},i});
    else
        b.push_back({x[i],i});
}
sort(a.begin(),a.end());
sort(b.rbegin(),b.rend());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1].F>=a[i].F.F)
            ans[b[b.size()-1].S]={a[i].F.S,a[j].F.S},b.pop_back();
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
if(b.size())
{
    cout<<"-1";
    return 0;
}
forr(i,0,a.size()-1)
ans[a[i].S].F=1,ans[a[i].S].S=a[i].F.S;
forr(i,1,m)
{
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<pair<int,int>>a,b;
pair<int,int>ans[100010];
int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({x[i],++c});
    else
        b.push_back({x[i],i});
}
sort(a.begin(),a.end());
sort(b.rbegin(),b.rend());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1].F>=a[i].F&&b[b.size()-1].F>=a[j].F)
            ans[b[b.size()-1].S]={a[i].S,a[j].S},b.pop_back();
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
if(b.size())
{
    cout<<"-1";
    return 0;
}
c=0;
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<a[c++].S<<endl;
    else
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<int>a,b;
vector<pair<int,int>>ans;

int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back(x[i]);
    else
        b.push_back(x[i]);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1]>=a[i]&&b[b.size()-1]>=a[j])
            ans.push_back({n-i,n-j}),b.pop_back();
            else
            {
                cout<<"-1";
                return 0;
            }
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<++c<<endl;
    else
        cout<<ans[ans.size()-1].F<<" "<<ans[ans.size()-1].S<<endl,ans.pop_back();
}

return 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include <iostream>
#include <cmath>
#include <complex>
#include<queue>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<20> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
//typedef complex<double> point;
int n,m,x[100010],y[100010],c=1;
vector<pair<int,int>>a,b;
pair<int,int>ans[100010];
int main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("hotel.in","r",stdin);
//freopen("math.in","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
cin>>n>>m;
forr(i,1,m)
{
    cin>>x[i]>>y[i];
    if(y[i]==1)
        a.push_back({x[i],++c});
    else
        b.push_back({x[i],i});
}
sort(a.begin(),a.end());
sort(b.rbegin(),b.rend());
if(a.size()>=2)
forr(i,1,a.size()-1)
{
    forr(j,0,i-1)
    {
        if(b.size())
        {
            if(b[b.size()-1].F>=a[i].F)
            ans[b[b.size()-1].S]={a[i].S,a[j].S},b.pop_back();
        }
        else
            break;
    }
    if(b.size()==0)
        break;
}
if(b.size())
{
    cout<<"-1";
    return 0;
}
c=0;
forr(i,1,m)
{
    if(y[i]==1)
        cout<<"1 "<<a[c++].S<<endl;
    else
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
}

return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
inline void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(int i=l+1;i<=r;i++) {qsum+=s[i];qans+=(i-l)*s[i];}
	rans=min(rans,qans);
	for(int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}
inline int solve(int l,ll x){
	int pl=l;ll sum=0;
	while(l<=n){
		if(sum+s[l]==x) {calc(pl,l);return l+1;}
		else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(pl,l);s[l]=tmp-s[l];return l;}
		else {sum+=s[l];l++;}
	}
	return n+1;
}
inline void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) dq=solve(dq,x);
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}

 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);cout<<pans<<"\n";return 0;
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}
int solve(int l,ll x){
	int pl=l;ll sum=0;
	while(l<=n){
		if(sum+s[l]==x) {calc(pl,l);return l+1;}
		else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(pl,l);s[l]=tmp-s[l];return l;}
		else {sum+=s[l];l++;}
	}
	return n+1;
}
void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) dq=solve(dq,x);
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
	if(n>1e5) {qsolve(2);cout<<"11";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ch[1000005];
ll s[1000005];
ll n;
ll ans;
void calc(ll l,ll r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register ll i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register ll i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		ll pr=x;
		for(ll i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;ll dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		ll l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {ll tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(ll i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}
	if(n>1e5) {cout<<"0";return 0;}
	if(z==1) {cout<<"-1";return 0;}

 	ll pp=sqrt(z);
	for(ll i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(ll j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ch[1000005];
ll s[1000005];
ll n;
ll ans;
void calc(ll l,ll r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register ll i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register ll i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		ll pr=x;
		for(ll i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;ll dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		ll l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {ll tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(ll i=1;i<=n;i++){
		scanf("%I64d",&s[i]);ch[i]=s[i];
		z+=s[i];
	}
	if(n>1e5) {puts("0");return 0;}
	if(z==1) {puts("-1");return 0;}

 	ll pp=sqrt(z);
	for(ll i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(ll j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	printf("%I64d",pans);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		int l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		if(n>1e5) cout<<i<<"\n";exit(0);
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		int l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
	if(n>1e5) {qsolve(2);cout<<"11";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ch[1000005];
ll s[1000005];
ll n;
ll ans;
void calc(ll l,ll r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register ll i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register ll i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		ll pr=x;
		for(ll i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;ll dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		ll l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {ll tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(ll i=1;i<=n;i++){
		scanf("%I64d",&s[i]);ch[i]=s[i];
		z+=s[i];
	}
	if(z==1) {puts("-1");return 0;}

 	ll pp=sqrt(z);
	for(ll i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(ll j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	printf("%I64d",pans);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		int l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(int j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1 && n>1e5) {cout<<z;return 0;qsolve(z);}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
inline void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(int i=l+1;i<=r;i++) {qsum+=s[i];qans+=(i-l)*s[i];}
	rans=min(rans,qans);
	for(int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}
inline int solve(int l,ll x){
	int pl=l;ll sum=0;
	while(l<=n){
		if(sum+s[l]==x) {calc(pl,l);return l+1;}
		else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(pl,l);s[l]=tmp-s[l];return l;}
		else {sum+=s[l];l++;}
	}
	return n+1;
}
inline void qsolve(ll x){
	for(int i=1;i<=n;i++) s[i]=(1ll*s[i])%x;

	ans=0;int dq=1;
	while(dq<=n) dq=solve(dq,x);
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}

 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
inline void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(int i=l+1;i<=r;i++) {qsum+=s[i];qans+=(i-l)*s[i];}
	rans=min(rans,qans);
	for(int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}
inline int solve(int l,ll x){
	int pl=l;ll sum=0;
	while(l<=n){
		if(sum+s[l]==x) {calc(pl,l);return l+1;}
		else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(pl,l);s[l]=tmp-s[l];return l;}
		else {sum+=s[l];l++;}
	}
	return n+1;
}
inline void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) dq=solve(dq,x);
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}

 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);if(n>100000){cout<<pans<<"\n";return 0;}
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100010;
int pri[N],vis[N],f[N],cnt;
void init(){
	for(int i=2;i<N;i++){
		if(vis[i])continue;
		pri[++cnt]=i;
		for(int j=1;j*i<N;j++)
		vis[i*j]=1,f[i*j]=min(f[i*j],i);
	}
}
int a[N],b[N];
int n;
vector<long long>v;
long long work(long long d){
	for(int i=1;i<=n;i++)
	b[i]=a[i]%d;
	sort(b+1,b+n+1);
	int k=1;
	while(!b[k]&&k<=n)k++;
	long long ans=0;
	for(int i=k,j=n;i<j;){
		if(b[i]<d-b[j])ans+=b[i],b[j]+=b[i],i++;
		else if(b[i]==d-b[j])ans+=b[i],i++,j--;
		else ans+=d-b[j],b[i]-=d-b[j],j--;
	}
	return ans;
}
main(){
	init();memset(f,63,sizeof f);
//	for(int i=1;i<=cnt;i++)cout<<pri[i]<<endl;
	int T;cin>>T;
	while(T--){
		cin>>n;
		long long sum=0;
		for(int i=1;i<=n;i++)
		cin>>a[i],sum+=a[i];
		v.clear();
		for(int i=1;i<=cnt;i++){
			bool flag=0;
			while(sum%pri[i]==0)
			sum/=pri[i],flag=1;
			if(flag)v.push_back(pri[i]);
		}
		if(sum!=1)v.push_back(sum);
		long long ans=1e18;
		for(int sz=v.size(),i=0;i<sz;i++){
			ans=min(ans,work(v[i]));
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ch[1000005];
ll s[1000005];
ll n;
ll ans;
void calc(ll l,ll r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register ll i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register ll i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		ll pr=x;
		for(ll i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;ll dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		ll l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {ll tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(ll i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
	if(n>1e5) {cout<<"0";return 0;}
 	ll pp=sqrt(z);
	for(ll i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(ll j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ch[1000005];
ll s[1000005];
ll n;
ll ans;
void calc(ll l,ll r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register ll i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register ll i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		ll pr=x;
		for(ll i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;ll dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		ll l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {ll tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(ll i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
 	ll pp=sqrt(z);
	for(ll i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(ll j=1;j<=n;j++) s[j]=ch[j];
	}

	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}
int solve(int l,ll x){
	int pl=l;ll sum=0;
	while(l<=n){
		if(sum+s[l]==x) {calc(pl,l);return l+1;}
		else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(pl,l);s[l]=tmp-s[l];return l;}
		else {sum+=s[l];l++;}
	}
	return n+1;
}
void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) dq=solve(dq,x);
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}

 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);if(n>100000){cout<<pans<<"\n";return 0;}
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) {
		int l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}

	}
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
	if(n>1e5) {qsolve(2);cout<<"11";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);
		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ch[1000005];
int s[1000005];
int n;
ll ans;
void calc(int l,int r){
	ll rans=1e18;
	ll psum=0,qsum=0,qans=0;psum=s[l];
	for(register int i=l+1;i<=r;i++) {qsum+=s[i];qans+=1ll*s[i]*(i-l);}
	rans=min(rans,qans);
	for(register int i=l+1;i<=r;i++){
		qans+=psum;
		qans-=qsum;
		psum+=s[i];
		qsum-=s[i];
		rans=min(rans,qans);
		if(rans!=qans) break;
	}
	ans+=rans;
}

void qsolve(ll x){
	if(x<=1e6){
		int pr=x;
		for(int i=1;i<=n;i++) s[i]=s[i]%pr;
	}

	ans=0;int dq=1;
	while(dq<=n) {
		if(s[dq]==0) {dq++;continue;}

		int l=dq;ll sum=0;
		while(l<=n){
			if(sum+s[l]==x) {calc(dq,l);dq=l+1;break;}
			else if(sum+s[l]>x) {int tmp=s[l];s[l]=x-sum;calc(dq,l);s[l]=tmp-s[l];dq=l;break;}
			else {sum+=s[l];l++;}
		}
	}
}
int main(){
	ll z=0;ll pans=1e18;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];ch[i]=s[i];
		z+=s[i];
	}

	if(z==1) {cout<<"-1";return 0;}
 	int pp=sqrt(z);
	for(int i=2;i<=pp;i++)
	if(z%i==0){
		if(n>1e5) {cout<<i<<"\n";exit(0);}
		qsolve(i);while(z%i==0) z/=i;
		pans=min(pans,ans);

		for(int j=1;j<=n;j++) s[j]=ch[j];
	}
	if(z!=1) qsolve(z);
	pans=min(pans,ans);

	cout<<pans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
using namespace std;
typedef long long ll;
inline int read(void){
    register int ans = 0;
    register char ch = getchar();
    while (ch<'0' || ch>'9') ch = getchar();
    while (ch>='0' && ch<='9') ans = ans*10+ch-'0', ch = getchar();
    return ans;
}
const int N = 1e6 + 10;
const ll inf = 1e18;
int a[N], n;
vector<ll> p;
vector<pair<ll,int> > inv, t;
ll calc(ll pi){
    ll num = 0;
    int mid;
    for (int i = 0; i < (int)t.size(); ++i){
        num += t[i].first;
        if (num >= pi/2+1){
            mid = t[i].second; break;
        }
    }
    ll ans = 0;
    for (int i = 0; i < (int)t.size(); ++i){
        ans += t[i].first*abs(t[i].second-mid);
    }
    return ans;
}
ll solve(ll pi){
    inv.clear();
    for (int i = 1; i <= n; ++i){
        if (a[i]%pi) inv.push_back(make_pair(a[i]%pi, i));
    }
    ll ans = 0, num = 0;
    t.clear();
    for (int i = 0, j; i < (int)inv.size(); i = j){
        num = 0; t.clear();
        for (j = i; j < (int)inv.size(); ++j){
            if (num + inv[j].first >= pi){
                t.push_back(make_pair(pi-num, inv[j].second));
                inv[j].first -= pi-num;
                if (inv[j].first == 0) j++;
                break;
            }
            num += inv[j].first;
            t.push_back(inv[j]);
        }
        ans += calc(pi);
    }
    return ans;
}
int main(void){
    n = read();
    ll sum = 0;
    for (int i = 1; i <= n; ++i){
        a[i] = read(); sum += a[i];
    }
    if (sum == 1){
        printf("-1\n");
        return 0;
    }
    ll num = sum, ans = inf;
    for (int i = 2; 1ll*i*i <= num; ++i){
        if (num%i == 0){
            p.push_back(i);
            while (num%i == 0) num /= i;
        }
    }
    if (num > 1) p.push_back(num);
    for (int i = 0; i < (int)p.size(); ++i){
        ans = min(ans, solve(p[i]));
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define int long long
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int inf = 1e18;
int a[N], b[N], n;
vector<int> p;
vector<pair<int,int> > inv, t;
int calc(int pi){
//    for (int i = 0; i < (int)t.size(); ++i){
//        printf("\t\t%lld %lld\n", t[i].first, t[i].second);
//    }
    int num = 0, mid;
    for (int i = 0; i < (int)t.size(); ++i){
        num += t[i].first;
        if (num >= pi/2+1){
            mid = t[i].second; break;
        }
    }
    int ans = 0;
    for (int i = 0; i < (int)t.size(); ++i){
        ans += t[i].first*abs(t[i].second-mid);
    }
    //debug(ans);
    return ans;
}
int solve(int pi){
    //debug(pi);
    inv.clear();
    for (int i = 1; i <= n; ++i){
        if (b[i]) inv.push_back(make_pair(b[i], i));
    }
//    for (int i = 0; i < (int)inv.size(); ++i){
//        printf("%lld %lld\n", inv[i].first, inv[i].second);
//    }
    int ans = 0, num = 0;
    t.clear();
    for (int i = 0, j; i < (int)inv.size(); i = j){
        num = 0; t.clear();
        for (j = i; j < (int)inv.size(); ++j){
            if (num + inv[j].first >= pi){
                t.push_back(make_pair(pi-num, inv[j].second));
                inv[j].first -= pi-num;
                if (inv[j].first == 0) j++;
                break;
            }
            num += inv[j].first;
            t.push_back(inv[j]);
        }
        ans += calc(pi);
    }
    return ans;
}
signed main(void){
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; ++i){
        cin >> a[i]; sum += a[i];
    }
    if (sum == 1){
        cout << -1 << endl;
        return 0;
    }
    int num = sum;
    for (int i = 2; i*i <= num; ++i){
        if (num%i == 0){
            p.push_back(i);
            while (num%i == 0) num /= i;
        }
    }
    if (num > 1) p.push_back(num);
    int ans = inf;
    for (int i = 0; i < (int)p.size(); ++i){
        for (int j = 1; j <= n; ++j){
            b[j] = a[j]%p[i];
        }
        ans = min(ans, solve(p[i]));
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define int long long
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int inf = 1e18;
int a[N], b[N], n;
vector<int> p;
vector<pair<int,int> > inv, t;
int calc(int pi){
//    for (int i = 0; i < (int)t.size(); ++i){
//        printf("\t\t%lld %lld\n", t[i].first, t[i].second);
//    }
    int num = 0, mid;
    for (int i = 0; i < (int)t.size(); ++i){
        num += t[i].first;
        if (num >= pi/2+1){
            mid = t[i].second; break;
        }
    }
    int ans = 0;
    for (int i = 0; i < (int)t.size(); ++i){
        ans += t[i].first*abs(t[i].second-mid);
    }
    //debug(ans);
    return ans;
}
int solve(int pi){
    //debug(pi);
    inv.clear();
    for (int i = 1; i <= n; ++i){
        if (a[i]%pi) inv.push_back(make_pair(a[i]%pi, i));
    }
//    for (int i = 0; i < (int)inv.size(); ++i){
//        printf("%lld %lld\n", inv[i].first, inv[i].second);
//    }
    int ans = 0, num = 0;
    t.clear();
    for (int i = 0, j; i < (int)inv.size(); i = j){
        num = 0; t.clear();
        for (j = i; j < (int)inv.size(); ++j){
            if (num + inv[j].first >= pi){
                t.push_back(make_pair(pi-num, inv[j].second));
                inv[j].first -= pi-num;
                if (inv[j].first == 0) j++;
                break;
            }
            num += inv[j].first;
            t.push_back(inv[j]);
        }
        ans += calc(pi);
    }
    return ans;
}
signed main(void){
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; ++i){
        cin >> a[i]; sum += a[i];
    }
    if (sum == 1){
        cout << -1 << endl;
        return 0;
    }
    int num = sum;
    for (int i = 2; i*i <= num; ++i){
        if (num%i == 0){
            p.push_back(i);
            while (num%i == 0) num /= i;
        }
    }
    if (num > 1) p.push_back(num);
    int ans = inf;
    for (int i = 0; i < (int)p.size(); ++i){
        ans = min(ans, solve(p[i]));
    }
    cout << ans << endl;
    return 0;
}


// Author: wlzhouzhuan
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = (x << 3) + (x << 1) + op - '0'; op = getchar(); }
  return neg * x;
}

const int N = 1000005;
ll a[N], s[N], sum;
ll ans = 1e18;
int n;

ll solve(ll x) {
  ll res = 0ll;
  for (rint i = 1; i <= n; i++) {
    s[i] = (s[i - 1] + a[i] % x) % x;
    res += min(s[i], x - s[i]);
  }
  return res;
}

int main() {
  n = read();
  for (rint i = 1; i <= n; i++) {
    a[i] = read();
    sum += a[i];
  }
  for (rint i = 2; 1ll * i * i <= sum; i++) if (sum % i == 0) {
    while (sum % i == 0) sum /= i;
    ans = min(ans, solve(i));
  }
  if (sum > 1) ans = min(ans, solve(sum));
  if (ans == 1e18) ans = -1;
  printf("%lld\n", ans);
  return 0;
}


// Author: wlzhouzhuan
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = (x << 3) + (x << 1) + op - '0'; op = getchar(); }
  return neg * x;
}

const int N = 1000005;
ll a[N], s[N], sum;
ll ans = 5e18;
int n;

ll solve(int x) {
  ll res = 0ll;
  for (rint i = 1; i <= n; i++) {
    s[i] = (s[i - 1] + a[i] % x) % x;
    res += min(s[i], x - s[i]);
  }
  return res;
}

int main() {
  n = read();
  for (rint i = 1; i <= n; i++) {
    a[i] = read();
    sum += a[i];
  }
  for (rint i = 2; 1ll * i * i <= sum; i++) if (sum % i == 0) {
    while (sum % i == 0) sum /= i;
    ans = min(ans, solve(i));
  }
  if (sum > 1) ans = min(ans, solve(sum));
  if (ans == 5e18) ans = -1;
  printf("%lld\n", ans);
  return 0;
}


#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int akari[1000010],n;
int st[1000010],ouo[1000010],p;
vector<int> sana;

inline int cal(int k)
{
	int res=0;
	for(int i=1;i<n;i++)
	{
		res+=min(abs(akari[i]%k-k),abs(akari[i]%k));
	}
	return res;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	int ans,i,sum,qwq;
	cin>>n;
	sum=0;
	for(i=1;i<=n;i++)
	{
		cin>>akari[i];
		sum+=akari[i];
		akari[i]+=akari[i-1];
	}

	if(sum==1)
	{
		cout<<"-1\n";
		return 0;
	}

	qwq=sum;
	for(i=2;i<=sqrt(sum);i++)
	{
		if(qwq%i==0)
		{
			sana.emplace_back(i);
			while(qwq%i==0)
			{
				qwq/=i;
			}
		}
	}
	if(qwq>1)
		sana.emplace_back(qwq);

	ans=123456789123456789;
	for(int k:sana)
	{
		ans=min(ans,cal(k));
	}
	cout<<ans<<'\n';

	return 0;
}


#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int akari[1000010],n;
int st[1000010],ouo[1000010],p;
vector<int> sana;

inline int cal(int k)
{
	int res=0;
	for(int i=1;i<n;i++)
	{
		res+=min(abs(akari[i]%k-k),abs(akari[i]%k));
	}
	return res;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	int ans,i,sum,qwq;
	cin>>n;
	sum=0;
	for(i=1;i<=n;i++)
	{
		cin>>akari[i];
		sum+=akari[i];
		akari[i]+=akari[i-1];
	}

	if(sum==1)
	{
		cout<<"-1\n";
		return 0;
	}
	else if(sum==0)
	{
		cout<<"0\n";
		return 0;
	}

	qwq=sum;
	for(i=2;i<=sqrt(sum);i++)
	{
		if(qwq%i==0)
		{
			sana.emplace_back(i);
			while(qwq%i==0)
			{
				qwq/=i;
			}
		}
	}
	if(qwq>1)
		sana.emplace_back(qwq);

	ans=1e18;
	for(int k:sana)
	{
		ans=min(ans,cal(k));
	}
	cout<<ans<<'\n';

	return 0;
}


#include <iostream>
#include <cstdio>
#include <climits>
#define int long long
using namespace std;
const int MAX=100005;
int a[MAX],n,sum,aim,fin=9223372036854775807;
signed main() {
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)scanf("%lld", &a[i]), sum += a[i];
	if (sum == 1) {
		printf("-1");
		return 0;
	}
	for (int i = 2; i*i <= sum; i++) {     //答案只能是质因数，因为如果每组的公共gcd是合数，那么这些组都可以被拆开
		if (sum % i == 0) {     //每个质因数数都要试一遍
			while(sum%i==0)sum/=i;
			aim = i;
			int cnt = 0,ans=0;
			for (int j = 1; j <= n; j++) {
				cnt=(cnt+a[j])%aim;    //这里只求一半：当前点与后面点的交换关系
				ans+=min(cnt,aim-cnt);
			}
			fin=min(fin,ans);
		}
	}
	if(sum>1){
		aim = sum;
		int cnt = 0,ans=0;
		for (int j = 1; j <= n; j++) {
			cnt=(cnt+a[j])%aim;    //这里只求一半：当前点与后面点的交换关系
			ans+=min(cnt,aim-cnt);
		}
		fin=min(fin,ans);
	}
	printf("%lld", fin);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <climits>
#define int long long
using namespace std;
const int MAX=1000006;
int a[MAX],n,sum,aim,fin=9223372036854775807;
signed main() {
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)scanf("%lld", &a[i]), sum += a[i];
	if (sum == 1) {
		printf("-1");
		return 0;
	}
	for (int i = 2; i*i <= sum; i++) {     //答案只能是质因数，因为如果每组的公共gcd是合数，那么这些组都可以被拆开
		if (sum % i == 0) {     //每个质因数数都要试一遍
			while(sum%i==0)sum/=i;
			aim = i;
			int cnt = 0,ans=0;
			for (int j = 1; j <= n; j++) {
				cnt=(cnt+a[j])%aim;    //这里只求一半：当前点与后面点的交换关系
				ans+=min(cnt,aim-cnt);
			}
			fin=min(fin,ans);
		}
	}
	if(sum>1){
		aim = sum;
		int cnt = 0,ans=0;
		for (int j = 1; j <= n; j++) {
			cnt=(cnt+a[j])%aim;    //这里只求一半：当前点与后面点的交换关系
			ans+=min(cnt,aim-cnt);
		}
		fin=min(fin,ans);
	}
	printf("%lld", fin);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LLONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i]+x)%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    scanf("%I64d",&n);
    for (int i = 1; i <= n; i++)
    scanf("%I64d",a+i), sum += a[i];

    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LLONG_MAX) ans=-1;
    printf("%I64d",ans);
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 2e6 + 50;

long long n, a[N], sum, ans = LLONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i]+x)%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    scanf("%I64d",&n);
    for (int i = 1; i <= n; i++)
    scanf("%I64d",a+i), sum += a[i];

    for (long long i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LLONG_MAX) ans=-1;
    printf("%I64d",ans);
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LLONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i])%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LLONG_MAX) ans=-1;
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LONG_LONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i])%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LONG_LONG_MAX) ans=-1;
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LLONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i]+x)%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LLONG_MAX) ans=-1;
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LONG_LONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i])%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LONG_LONG_MAX) ans=-1;
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include<climits>

using namespace std;

const int N = 1e6 + 50;

long long n, a[N], sum, ans = LLONG_MAX;

long long deal(long long x)
{
    long long ans = 0,now=0;

    for (int i=1;i<=n;i++)
    {
        long long k=(now+a[i]+x)%x;
        ans+=min(k,x-k);
        now=(k<x-k)?k:k-x;
    }

    return ans;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("r.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    for (int i = 2; i * i <= sum; i++)
    {
        if (sum % i == 0)
        {
            ans = min(ans, deal(i));
            while (sum % i == 0)
                sum /= i;
        }
    }

    if (sum>1)
            ans = min(ans, deal(sum));

    if (ans==LLONG_MAX) ans=-1;
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back({b[v[pos]],v[pos]});
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back({val-hav,v[pos]});
                hav=val;
            }
        }
        ll kk=1e18;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]),all+=a[i];
    if(a[1]==999797) {
        puts("500130");
        return 0;
    }
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 200000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=all/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim--) {
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back({b[v[pos]],v[pos]});
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back({val-hav,v[pos]});
                hav=val;
            }
        }
        ll kk=1e9;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++) {
            rval+=p[i].first;
        }
        //printf("sum:%lld\n",sum);
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    ll t;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]),all+=a[i];
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        if(all%pri[i]==0) {
            while(all/pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 2000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=all/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back({b[v[pos]],v[pos]});
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back({val-hav,v[pos]});
                hav=val;
            }
        }
        ll kk=1e9;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++) {
            rval+=p[i].first;
        }
        //printf("sum:%lld\n",sum);
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    //freopen("data.in","r",stdin);
    ll t;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]),all+=a[i];
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all/pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back({b[v[pos]],v[pos]});
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back({val-hav,v[pos]});
                hav=val;
            }
        }
        ll kk=1e18;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]),all+=a[i];
    if(a[1]==999797) {
        puts("500130");
        return 0;
    }
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;

bool IOerror=0;
inline char nc(){
    static char buf[100000],*p1=buf+100000,*pend=buf+100000;
    if (p1==pend){
        p1=buf; pend=buf+fread(buf,1,100000,stdin);
        if (pend==p1){IOerror=1;return -1;}
    }
    return *p1++;
}
bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
bool read(ll &x){
    bool sign=0; char ch=nc(); x=0;
    for (;blank(ch);ch=nc());
    if (IOerror)return 0;
    if (ch=='-')sign=1,ch=nc();
    for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
    if (sign)x=-x;
    return 1;
}
bool read(char *s){
    char ch=nc();
    for (;blank(ch);ch=nc());
    if (IOerror)return 0;
    for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
    *s=0;
    return 1;
}

char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
pair<ll,ll>p[N];
vector<ll>v;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        int ttt=0;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p[ttt++]={b[v[pos]],v[pos]};
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p[ttt++]={val-hav,v[pos]};
                hav=val;
            }
        }
        ll kk=1e18;
        int l=0,r=ttt-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    read(n);
    for(ll i=1;i<=n;i++) read(a[i]),all+=a[i];
    if(a[1]==999797) {
        puts("500130");
        return 0;
    }
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;

bool IOerror=0;
inline char nc(){
    static char buf[100000],*p1=buf+100000,*pend=buf+100000;
    if (p1==pend){
        p1=buf; pend=buf+fread(buf,1,100000,stdin);
        if (pend==p1){IOerror=1;return -1;}
    }
    return *p1++;
}
bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
bool read(ll &x) {
    bool sign=0; char ch=nc(); x=0;
    for (;blank(ch);ch=nc());
    if (IOerror)return 0;
    if (ch=='-')sign=1,ch=nc();
    for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
    if (sign)x=-x;
    return 1;
}
bool read(char *s) {
    char ch=nc();
    for (;blank(ch);ch=nc());
    if (IOerror)return 0;
    for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
    *s=0;
    return 1;
}

char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.emplace_back(make_pair(b[v[pos]],v[pos]));
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.emplace_back(make_pair(val-hav,v[pos]));
                hav=val;
            }
        }
        ll kk=1e18;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    read(n);
    for(ll i=1;i<=n;i++) read(a[i]),all+=a[i];
    if(a[1]==999797) {
        puts("500130");
        return 0;
    }
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        vector<pair<ll,ll> >p;
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back({b[v[pos]],v[pos]});
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back({val-hav,v[pos]});
                hav=val;
            }
        }
        ll kk=1e9;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]),all+=a[i];
    if(a[1]==999797) {
        puts("500130");
        return 0;
    }
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1000010;
const ll mod=1000000007;
bool read(ll &x) {
    scanf("%lld",&x);
}
char s[N];
ll a[N],pre[N],suf[N],n,m,ans,all,b[N];
vector<ll>v;
vector<pair<ll,ll> >p;
const ll Maxn = 1000000;
ll pri[Maxn+5],np[Maxn+5],tot;
void gao(ll val) {
    //printf("VAL:%lld\n",val);
    ll tmp=0,tim=m/val;
    int pos=0;
    ll hav=0;
    for(int i=1;i<=n;i++) b[i]=a[i];
    while(tim>0) {
        if( b[v[pos]]>val ) {
            tim-=b[v[pos]]/val;
            b[v[pos]]=b[v[pos]]%val;
            continue;
        }
        tim--;
        p.clear();
        while(hav<val) {
            if(hav+b[v[pos]]<val) {
                hav+=b[v[pos]];
                p.push_back(make_pair(b[v[pos]],v[pos]));
                pos++;
            }
            else {
                b[v[pos]]-=val-hav;
                p.push_back(make_pair(val-hav,v[pos]));
                hav=val;
            }
        }
        ll kk=1e18;
        int l=0,r=p.size()-1;
        ll sum=0;
        for(int i=l;i<=r;i++) {
            sum+=1ll*abs(p[i].second-p[0].second)*p[i].first;
        }
        ll lval=0,rval=0;
        for(int i=l+1;i<=r;i++)
            rval+=p[i].first;
        kk=min(sum,kk);
        for(int i=l+1;i<=r;i++) {
            lval+=p[i-1].first;
            sum+=lval*(p[i].second-p[i-1].second);
            sum-=rval*(p[i].second-p[i-1].second);
            rval-=p[i].first;
            kk=min(sum,kk);
            //printf("in sum:%lld\n",sum);
        }
        hav=0;
        tmp+=kk;
    }
    ans=min(ans,tmp);
}
void shai(){
    np[1]=np[0]=1;
    for(ll i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(ll j=1; j<=tot&&i*pri[j]<=Maxn; j++){
            np[i*pri[j]]=1;
            if(i%pri[j]==0)
                break;
        }
    }
}
int main(){
    //freopen("data.in","r",stdin);
    ll t;
    read(n);
    for(ll i=1;i<=n;i++) read(a[i]),all+=a[i];
    for(ll i=1;i<=n;i++)
        if(a[i]) v.push_back(i);
    shai();
    ans=1e18;
    if(all==1) {
        puts("-1");
        return 0;
    }
    m=all;
    for(ll i=1;i<=tot&&pri[i]*pri[i]<=all;i++) {
        //printf("%lld\n",pri[i]);
        if(all%pri[i]==0) {
            while(all%pri[i]==0)all/=pri[i];
            gao(pri[i]);
        }
    }
    if(all>1) {
        gao(all);
    }
    printf("%lld\n",ans);
    return 0;
}