#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];
    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];
int a[N],n,m,b[N];

ll ans,all;
vector<int>v;
const ll Maxn = 1000000;
int pri[Maxn+5],np[Maxn+5],tot;
inline 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);
}
inline void shai(){
    np[1]=np[0]=1;
    for(int i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(int 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);
    scanf("%d",&n);
    for(ll i=1;i<=n;i++) scanf("%d",&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&&1ll*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];
pair<ll,ll>p[N];
ll v[N];
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,tt=0;
    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[tt++]=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;
}


#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];
int a[N],n,m,all,b[N];
ll ans;
vector<int>v;
const ll Maxn = 1000000;
int pri[Maxn+5],np[Maxn+5],tot;
inline 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);
}
inline void shai(){
    np[1]=np[0]=1;
    for(int i=2; i<=Maxn; i++){
        if(!np[i])
            pri[++tot]=i;
        for(int 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&&1ll*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(){
    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;
}


#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;
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;
}


#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];
    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 = 2000000;
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;
        }
        //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;

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.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;
    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;
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.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;
}


#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];
ll v[N];
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,tt=0;
    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[tt++]=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 = 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;
}


#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;
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.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];
    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;

#define REP(i, a, b) for (register int i = (a); i <= (b); ++i)
#define RREP(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, a, b) for (register int i = (a); i < (b); ++i)
#define what_is(x) cerr << #x << " is " << x << endl;
#define cl(x) memset(x, 0, sizeof(x))
// #define rson m + 1, r, rt << 1 | 1
// #define lson l, m, rt << 1
#define sqr(x) ((x) * (x))
#define cl(x) memset(x, 0, sizeof(x))

const int maxn = 1000000 + 1000;
// const int maxn = 50;
const int maxm = 200000 + 100;
// const int maxm = 100;
const int maxt = 3000000 + 100;
const int maxk = 10 + 3;
const int M = 2010;

typedef long long LL;
typedef long double LD;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pll;

const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL Inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-3;
const double inf = 1e15;
const double pi = acos(-1.0);
// const LL mod = 1000000007LL;
// const LL mod = 1004535809LL, gg = 3LL, inv2 = 502267905LL;
// const LL mod = 167772161LL, gg = 3LL, inv2 = 83886081LL;
const LL mod = 998244353LL, gg = 3LL, inv2 = 499122177LL;

inline LL read()
{
	LL x = 0;
	bool t = false;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-')
		ch = getchar();
	if (ch == '-')
		t = true, ch = getchar();
	while (ch <= '9' && ch >= '0')
		x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}

LL ans;
int n, a[maxn], b[maxn];
vector<int> tmp;

void Gao(int m)
{
	for (int i = 1; i <= n; ++i)
		b[i] = a[i] % m;
	LL res = 0;
	for (int i = 1; i <= n; ++i)
	{
		int s = 0, j = i;
		tmp.clear();
		while (j <= n && s < m)
		{
			tmp.push_back(b[j]);
			s += b[j];
			++j;
		}
		if ((j == n + 1) &&(s < m))
			break;
		--j;
		i = j;
		b[j] = s - m;
		if (b[j])
			--i;
		tmp[tmp.size() - 1] -= s - m;
		int r = (m - 1) / 2 + 1;
		LL sum = 0, mid = -1;
		for (int i = 0; i < tmp.size(); ++i)
		{
			sum += tmp[i];
			if (sum >= r)
			{
				mid = i;
				break;
			}
		}
		for (int i = 0; i < tmp.size(); ++i)
			res += abs(mid - i) * tmp[i];
	}
	ans = min(ans, res);
}

int main()
{
	// ios::sync_with_stdio(false);
	// cin.tie(0);
	// freopen("C:\\Users\\WA_TERMINATOR\\Desktop\\Helloworld\\input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	n = read();
	LL cnt = 0;
	for (int i = 1; i <= n; ++i)
		a[i] = read(), cnt += a[i];
	if (cnt == 1)
	{
		printf("-1\n");
		return 0;
	}
	ans = Inf;
	int lim = (int)sqrt(cnt + 0.5);
	for (int i = 2; i <= lim; ++i)
	{
		if (cnt % i)
			continue;
		Gao(i);
		while (cnt % i == 0)
			cnt /= i;
	}
	if (cnt > 1)
		Gao(cnt);
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (register int i = (a); i <= (b); ++i)
#define RREP(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, a, b) for (register int i = (a); i < (b); ++i)
#define what_is(x) cerr << #x << " is " << x << endl;
#define cl(x) memset(x, 0, sizeof(x))
// #define rson m + 1, r, rt << 1 | 1
// #define lson l, m, rt << 1
#define sqr(x) ((x) * (x))
#define cl(x) memset(x, 0, sizeof(x))

const int maxn = 1000000 + 1000;
// const int maxn = 50;
const int maxm = 200000 + 100;
// const int maxm = 100;
const int maxt = 3000000 + 100;
const int maxk = 10 + 3;
const int M = 2010;

typedef long long LL;
typedef long double LD;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pll;

const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL Inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-3;
const double inf = 1e15;
const double pi = acos(-1.0);
// const LL mod = 1000000007LL;
// const LL mod = 1004535809LL, gg = 3LL, inv2 = 502267905LL;
// const LL mod = 167772161LL, gg = 3LL, inv2 = 83886081LL;
const LL mod = 998244353LL, gg = 3LL, inv2 = 499122177LL;

inline LL read()
{
	LL x = 0;
	bool t = false;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-')
		ch = getchar();
	if (ch == '-')
		t = true, ch = getchar();
	while (ch <= '9' && ch >= '0')
		x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}

LL ans;
int n, a[maxn], b[maxn];
vector<int> tmp;

void Gao(int m)
{
	for (int i = 1; i <= n; ++i)
		b[i] = a[i] % m;
	LL res = 0;
	for (int i = 1; i <= n; ++i)
	{
		int s = 0, j = i;
		tmp.clear();
		while (j <= n && s < m)
		{
			tmp.push_back(b[j]);
			s += b[j];
			++j;
		}
		if ((j == n + 1) &&(s < m))
			break;
		--j;
		i = j;
		b[j] = s - m;
		if (b[j])
			--i;
		tmp[tmp.size() - 1] -= s - m;
		int r = (m - 1) / 2 + 1;
		LL sum = 0, mid = -1;
		for (int i = 0; i < tmp.size(); ++i)
		{
			sum += tmp[i];
			if (sum >= r)
			{
				mid = i;
				break;
			}
		}
		for (int i = 0; i < tmp.size(); ++i)
			res += abs(mid - i) * tmp[i];
	}
	ans = min(ans, res);
}

int main()
{
	// ios::sync_with_stdio(false);
	// cin.tie(0);
	// freopen("C:\\Users\\WA_TERMINATOR\\Desktop\\Helloworld\\input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	n = read();
	int cnt = 0;
	for (int i = 1; i <= n; ++i)
		a[i] = read(), cnt += a[i];
	if (cnt == 1)
	{
		printf("-1\n");
		return 0;
	}
	ans = Inf;
	int lim = (int)sqrt(cnt + 0.5);
	for (int i = 2; i <= lim; ++i)
	{
		if (cnt % i)
			continue;
		Gao(i);
		while (cnt % i == 0)
			cnt /= i;
	}
	if (cnt > 1)
		Gao(cnt);
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (register int i = (a); i <= (b); ++i)
#define RREP(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, a, b) for (register int i = (a); i < (b); ++i)
#define what_is(x) cerr << #x << " is " << x << endl;
#define cl(x) memset(x, 0, sizeof(x))
// #define rson m + 1, r, rt << 1 | 1
// #define lson l, m, rt << 1
#define sqr(x) ((x) * (x))
#define cl(x) memset(x, 0, sizeof(x))

const int maxn = 1000000 + 1000;
// const int maxn = 50;
const int maxm = 200000 + 100;
// const int maxm = 100;
const int maxt = 3000000 + 100;
const int maxk = 10 + 3;
const int M = 2010;

typedef long long LL;
typedef long double LD;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pll;

const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL Inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-3;
const double inf = 1e15;
const double pi = acos(-1.0);
// const LL mod = 1000000007LL;
// const LL mod = 1004535809LL, gg = 3LL, inv2 = 502267905LL;
// const LL mod = 167772161LL, gg = 3LL, inv2 = 83886081LL;
const LL mod = 998244353LL, gg = 3LL, inv2 = 499122177LL;

inline LL read()
{
	LL x = 0;
	bool t = false;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-')
		ch = getchar();
	if (ch == '-')
		t = true, ch = getchar();
	while (ch <= '9' && ch >= '0')
		x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}

LL ans;
LL n, a[maxn], b[maxn];
vector<LL> tmp;

void Gao(LL m)
{
	for (LL i = 1; i <= n; ++i)
		b[i] = a[i] % m;
	LL res = 0;
	for (LL i = 1; i <= n; ++i)
	{
		LL s = 0, j = i;
		tmp.clear();
		while (j <= n && s < m)
		{
			tmp.push_back(b[j]);
			s += b[j];
			++j;
		}
		if ((j == n + 1) && (s < m))
			break;
		--j;
		i = j;
		b[j] = s - m;
		if (b[j])
			--i;
		if (!tmp.empty())
			tmp[tmp.size() - 1] -= s - m;
		LL r = (m - 1) / 2 + 1;
		LL sum = 0, mid = -1;
		for (LL i = 0; i < tmp.size(); ++i)
		{
			sum += tmp[i];
			if (sum >= r)
			{
				mid = i;
				break;
			}
		}
		for (LL i = 0; i < tmp.size(); ++i)
			res += abs(mid - i) * tmp[i];
	}
	ans = min(ans, res);
}

int main()
{
	// ios::sync_with_stdio(false);
	// cin.tie(0);
	// freopen("C:\\Users\\WA_TERMINATOR\\Desktop\\Helloworld\\input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	n = read();
	LL cnt = 0;
	for (LL i = 1; i <= n; ++i)
		a[i] = read(), cnt += a[i];
	if (cnt == 1)
	{
		printf("-1\n");
		return 0;
	}
	ans = Inf;
	LL lim = (LL)sqrt(cnt + 0.5);
	for (LL i = 2; i <= lim; ++i)
	{
		if (cnt % i)
			continue;
		Gao(i);
		while (cnt % i == 0)
			cnt /= i;
	}
	if (cnt > 1)
		Gao(cnt);
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 1e6;
int n, a[N + 5];
long long sum;

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

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]), sum += a[i];
	if (sum == 1) return puts("-1"), 0;
	long long ans = 1LL << 60;
	for (long long i = 2; i * i <= sum; i++)
		if (sum % i == 0) {
			ans = min(ans, solve(i));
			while (sum % i == 0) sum /= i;
		}
	if (sum > 1) ans = min(ans, solve(sum));
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 1e6;
int n, a[N + 5];
long long sum;

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

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]), sum += a[i];
	if (sum == 1) return puts("-1"), 0;
	long long ans = 1LL << 60;
	for (long long i = 2; i * i <= sum; i++)
		if (sum % i == 0) {
			ans = min(ans, solve(i));
			while (sum % i == 0) sum /= i;
		}
	printf("%lld\n", ans);
	return 0;
}


// Problem : E1. Send Boxes to Alice (Easy Version)
// Contest : Codeforces - Codeforces Round #601 (Div. 2)
// URL : https://codeforces.ml/contest/1255/problem/E1
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e6+7;
ll a[N],res = -1;
int n;
void work(ll k)
{
	ll tmp = 0;
	for(int i = 1;i < n;++i)
		tmp += min(a[i] % k,k - a[i] % k);
	if(res == -1)	res = tmp;
	else res = min(res,tmp);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;++i)	scanf("%lld",&a[i]),a[i] += a[i - 1];
	for(ll i = 2;i <= a[n] / i;++i)
	{
		if(a[n] % i == 0)
		{
			work(i);
			while(a[n] % i == 0)	a[n] /= i;
		}
	}
	if(a[n] != 1)	work(a[n]);
	printf("%lld",res);
    return 0;
}


// Problem : E1. Send Boxes to Alice (Easy Version)
// Contest : Codeforces - Codeforces Round #601 (Div. 2)
// URL : https://codeforces.ml/contest/1255/problem/E1
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e6+7;
ll a[N],res = -1;
int n;
void work(int k)
{
	ll tmp = 0;
	for(int i = 1;i < n;++i)
		tmp += min(a[i] % k,k - a[i] % k);
	if(res == -1)	res = tmp;
	else res = min(res,tmp);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;++i)	scanf("%lld",&a[i]),a[i] += a[i - 1];
	for(ll i = 2;i <= a[n] / i;++i)
	{
		if(a[n] % i == 0)
		{
			work(i);
			while(a[n] % i == 0)	a[n] /= i;
		}
	}
	if(n != 1)	work(n);
	printf("%lld",res);
    return 0;
}


// Problem : E1. Send Boxes to Alice (Easy Version)
// Contest : Codeforces - Codeforces Round #601 (Div. 2)
// URL : https://codeforces.ml/contest/1255/problem/E1
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e6+7;
ll a[N],res = -1;
int n;
void work(int k)
{
	ll tmp = 0;
	for(int i = 1;i < n;++i)
		tmp += min(a[i] % k,k - a[i] % k);
	if(res == -1)	res = tmp;
	else res = min(res,tmp);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;++i)	scanf("%lld",&a[i]),a[i] += a[i - 1];
	for(ll i = 2;i <= a[n] / i;++i)
	{
		if(a[n] % i == 0)
		{
			work(i);
			while(a[n] % i == 0)	a[n] /= i;
		}
	}
	if(a[n] != 1)	work(a[n]);
	printf("%lld",res);
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = int(1e6)+7;
int a[MAXN];
long long ss[MAXN];
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	long long s=0;
	for(int i=0; i<n; i++) cin >> a[i];
	ss[0]=a[0];
	for(int i=1; i<n; i++) ss[i]=a[i]+ss[i-1];
	s=ss[n-1];
	long long ans=0x7f7f7f7f7f7f7f7fll;
	for(long long now=2; now*now<=s; now++) if(s%now==0) {
		long long nowans=0;
		while(s%now==0) s/=now;
		for(int i=0; i<n-1; i++) nowans+=min(ss[i]%now, now-ss[i]%now);
		ans=min(ans,nowans);
		s/=now;
	}
	if(ans<0x7f7f7f7f7f7f7f7fll) cout << ans << '\n';
	else cout << "-1\n";
}


#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = int(1e6)+7;
int pre[MAXN];
int a[MAXN];
inline void db() {
	memset(pre,0,sizeof pre);
	for(int i=2; i<=int(1e6); i++) {
		if(!pre[i]) pre[i]=i;
		if(i<(int(1e6)+i-1)/i)
		for(int j=i*i; j<=int(1e6); j+=i) {
			pre[j]=i;
		}
	}
}
long long ss[MAXN];
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	db();
	int n; cin >> n;
	long long s=0;
	for(int i=0; i<n; i++) cin >> a[i];
	ss[0]=a[0];
	for(int i=1; i<n; i++) ss[i]=a[i]+ss[i-1];
	s=ss[n-1];
	long long ans=0x7f7f7f7f7f7f7f7fll;
	while(s>1) {
		long long nowans=0;
		int now=pre[s];
		while(s%now==0) s/=now;
		for(int i=0; i<n-1; i++) nowans+=min(ss[i]%now, now-ss[i]%now);
		ans=min(ans,nowans);
	}
	if(ans<0x7f7f7f7f7f7f7f7fll) cout << ans << '\n';
	else cout << "-1\n";
}


#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = int(1e6)+7;
int a[MAXN];
long long ss[MAXN];
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	long long s=0;
	for(int i=0; i<n; i++) cin >> a[i];
	ss[0]=a[0];
	for(int i=1; i<n; i++) ss[i]=a[i]+ss[i-1];
	s=ss[n-1];
	long long ans=0x7f7f7f7f7f7f7f7fll;
	for(long long now=2; now*now<=s; now++) if(s%now==0) {
		long long nowans=0;
		for(int i=0; i<n-1; i++) nowans+=min(ss[i]%now, now-ss[i]%now);
		ans=min(ans,nowans);
		while(s%now==0) s/=now;
	}
	if(s>1) {
		long long nowans=0;
		for(int i=0; i<n-1; i++) nowans+=min(ss[i]%s, s-ss[i]%s);
		ans=min(ans,nowans);
	}
	if(ans<0x7f7f7f7f7f7f7f7fll) cout << ans << '\n';
	else cout << "-1\n";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000005
#define maxm 200005
#define INF 1234567890000
#define p 1000000007
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline T maxd(T a,T b){if(b>a)return b;return a;}
template<class T>inline T mind(T a,T b){if(b<a)return b;return a;}
template<class T>inline T gcd(T a,T b){while(a^=b^=a^=b%=a);return b;}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline T absd(const T &a){if(a<0)return -a;return a;}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
ll n,sum,a[maxn],b[maxn],ans=INF;
ll Solve(ll k)
{
    for(ll i=1;i<=n;i++)
        b[i]=a[i]%k;
    ll res=0,tot=0,l=0;
    for(ll i=1;i<=n;i++)
    {
        if(tot+b[i]>=k)
        {
            ll x=tot+b[i]-k,s=0;
            b[i]=k-tot;
            for(ll j=l,r=i;j<r;)
            {
                if(b[j]<b[r])
                {
                    b[j+1]+=b[j];
                    s+=b[j];
                    j++;
                }
                else
                {
                    b[r-1]+=b[r];
                    s+=b[r];
                    r--;
                }
            }
            res+=s;
            b[i]=x;
            tot=x;
            l=i;
        }
        else tot+=b[i];
    }
    return res;
}
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(a[i]);
        sum+=a[i];
    }
    for(ll i=2;i<=sum;i++)
    {
        if(sum%i)continue;
        while(sum%i==0)sum/=i;
        ans=mind(ans,Solve(i));
    }
    if(sum>1)ans=mind(ans,Solve(sum));
    if(ans<INF)print(ans,'\n');
    else print(-1,'\n');
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000005
#define maxm 200005
#define INF 987654321987654321
#define p 1000000007
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline T maxd(T a,T b){if(b>a)return b;return a;}
template<class T>inline T mind(T a,T b){if(b<a)return b;return a;}
template<class T>inline T gcd(T a,T b){while(a^=b^=a^=b%=a);return b;}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline T absd(const T &a){if(a<0)return -a;return a;}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
ll n,sum,a[maxn],b[maxn],ans=INF;
ll Solve(ll k)
{
    for(ll i=1;i<=n;i++)
        b[i]=a[i]%k;
    ll res=0,tot=0,l=0;
    for(ll i=1;i<=n;i++)
    {
        if(tot+b[i]>=k)
        {
            ll x=tot+b[i]-k,s=0;
            b[i]=k-tot;
            for(ll j=l,r=i;j<r;)
            {
                if(b[j]<b[r])
                {
                    b[j+1]+=b[j];
                    s+=b[j];
                    j++;
                }
                else
                {
                    b[r-1]+=b[r];
                    s+=b[r];
                    r--;
                }
            }
            res+=s;
            b[i]=x;
            tot=x;
            l=i;
        }
        else tot+=b[i];
    }
    return res;
}
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(a[i]);
        sum+=a[i];
    }
    for(ll i=2;i*i<=sum;i++)
    {
        if(sum%i)continue;
        while(sum%i==0)sum/=i;
        ans=mind(ans,Solve(i));
    }
    if(sum>1)ans=mind(ans,Solve(sum));
    if(ans<INF)print(ans,'\n');
    else print(-1,'\n');
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000005
#define maxm 200005
#define INF 1234567890000
#define p 1000000007
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline T maxd(T a,T b){if(b>a)return b;return a;}
template<class T>inline T mind(T a,T b){if(b<a)return b;return a;}
template<class T>inline T gcd(T a,T b){while(a^=b^=a^=b%=a);return b;}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline T absd(const T &a){if(a<0)return -a;return a;}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
ll n,sum,a[maxn],b[maxn],ans=INF;
ll Solve(ll k)
{
    for(ll i=1;i<=n;i++)
        b[i]=a[i]%k;
    ll res=0,tot=0,l=0;
    for(ll i=1;i<=n;i++)
    {
        if(tot+b[i]>=k)
        {
            ll x=tot+b[i]-k,s=0;
            b[i]=k-tot;
            for(ll j=l,r=i;j<r;)
            {
                if(b[j]<b[r])
                {
                    b[j+1]+=b[j];
                    s+=b[j];
                    j++;
                }
                else
                {
                    b[r-1]+=b[r];
                    s+=b[r];
                    r--;
                }
            }
            res+=s;
            b[i]=x;
            tot=x;
            l=i;
        }
        else tot+=b[i];
    }
    return res;
}
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(a[i]);
        sum+=a[i];
    }
    for(ll i=2;i*i<=sum;i++)
    {
        if(sum%i)continue;
        while(sum%i==0)sum/=i;
        ans=mind(ans,Solve(i));
    }
    if(sum>1)ans=mind(ans,Solve(sum));
    if(ans<INF)print(ans,'\n');
    else print(-1,'\n');
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 100005
#define maxm 200005
#define INF 1234567890000
#define p 1000000007
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline T maxd(T a,T b){if(b>a)return b;return a;}
template<class T>inline T mind(T a,T b){if(b<a)return b;return a;}
template<class T>inline T gcd(T a,T b){while(a^=b^=a^=b%=a);return b;}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline T absd(const T &a){if(a<0)return -a;return a;}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
ll n,sum,a[maxn],b[maxn],ans=INF;
ll Solve(ll k)
{
    for(ll i=1;i<=n;i++)
        b[i]=a[i]%k;
    ll res=0,tot=0,l=0;
    for(ll i=1;i<=n;i++)
    {
        if(tot+b[i]>=k)
        {
            ll x=tot+b[i]-k,s=0;
            b[i]=k-tot;
            for(ll j=l,r=i;j<r;)
            {
                if(b[j]<b[r])
                {
                    b[j+1]+=b[j];
                    s+=b[j];
                    j++;
                }
                else
                {
                    b[r-1]+=b[r];
                    s+=b[r];
                    r--;
                }
            }
            res+=s;
            b[i]=x;
            tot=x;
            l=i;
        }
        else tot+=b[i];
    }
    return res;
}
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(a[i]);
        sum+=a[i];
    }
    for(ll i=2;i<=sum;i++)
    {
        if(sum%i)continue;
        while(sum%i==0)sum/=i;
        ans=mind(ans,Solve(i));
    }
    if(sum>1)ans=mind(ans,Solve(sum));
    if(ans<INF)print(ans,'\n');
    else print(-1,'\n');
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
long long pref[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
    pref[i] = sum;
  }

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

  auto try_for_divisor = [&](int divisor) {
    long long cur_ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      long long rem = pref[i] % divisor;
      cur_ans += min(rem , -rem + divisor);
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i * 1ll * i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      try_for_divisor(i);
    }
  }

  if (sum > 1) {
    try_for_divisor(sum);
  }
  cout << ans << "\n";
  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
int a_for_iteration[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

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

  auto try_for_divisor = [&](long long divisor) {
    // cout << "try for divisor: " << divisor << endl;
    long long cur_ans = 0;
    int l = 0;
    int r = 0;

    long long left_sum = 0;
    long long left_cnt = 0;

    while (l < n && r < n) {
      if (left_cnt + a_for_iteration[r] < divisor) {
        left_cnt += a_for_iteration[r++];
        left_sum += left_cnt;
      } else {
        long long right_sum = 0;
        long long min_res = left_sum + right_sum;

        long long right_cnt = divisor - left_cnt;
        right_sum += right_cnt;
        a_for_iteration[r] -= right_cnt;

        for (int j = r - 1; j >= l; --j) {
          left_sum -= left_cnt;
          left_cnt -= a_for_iteration[j];
          min_res = min(min_res, left_sum + right_sum);

          right_cnt += a_for_iteration[j];
          right_sum += right_cnt;
        }
        cur_ans += min_res;
        l = r;
      }
    }
    ans = min(ans, cur_ans);
  };

  for (long long i = 2; i * i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      for (int j = 0; j < n; ++j) {
        a_for_iteration[j] = a[j] % i;
      }
      try_for_divisor(i);
    }
  }

  if (sum > 1) {
    for (int j = 0; j < n; ++j) {
      a_for_iteration[j] = a[j] % sum;
    }
    try_for_divisor(sum);
  }
  cout << ans << "\n";

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
int a_for_iteration[MAXN];

int main() {
  int n;
  int sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  auto try_for_divisor = [&](int divisor) {
    // cout << "try for divisor: " << divisor << endl;
    long long cur_ans = 0;
    int l = 0;
    int r = 0;

    long long left_sum = 0;
    long long left_cnt = 0;

    while (l < n && r < n) {
      if (left_cnt + a_for_iteration[r] < divisor) {
        left_cnt += a_for_iteration[r++];
        left_sum += left_cnt;
      } else {
        long long right_sum = 0;
        long long min_res = left_sum + right_sum;

        long long right_cnt = divisor - left_cnt;
        right_sum += right_cnt;
        a_for_iteration[r] -= right_cnt;

        for (int j = r - 1; j >= l; --j) {
          left_sum -= left_cnt;
          left_cnt -= a_for_iteration[j];
          min_res = min(min_res, left_sum + right_sum);

          right_cnt += a_for_iteration[j];
          right_sum += right_cnt;
        }
        cur_ans += min_res;
        l = r;
      }
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      for (int j = 0; j < n; ++j) {
        a_for_iteration[j] = a[j] % i;
      }
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
long long pref[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
    pref[i] = sum;
  }

  auto try_for_divisor = [&](int divisor) {
    long long cur_ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      int rem = pref[i] % divisor;
      cur_ans += min(rem , divisor - rem);
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i * i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e5 + 5;
const long long INF = 1000000000ll * 1000000000ll;
vector<int> a;
vector<int> a_for_iteration;

int main() {
  int n;
  int sum = 0;
  long long ans = INF;

  cin >> n;
  a.resize(n);
  a_for_iteration.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }

  auto try_for_divisor = [&](int divisor) {
    // cout << "try for divisor: " << divisor << endl;
    long long cur_ans = 0;
    int l = 0;
    int r = 0;

    long long left_sum = 0;
    long long left_cnt = 0;

    while (l < n && r < n) {
      if (left_cnt + a_for_iteration[r] < divisor) {
        left_cnt += a_for_iteration[r++];
        left_sum += left_cnt;
        //cout << "(" << l << ","<< r << "): left_cnt: " << left_cnt << ", left_sum: " << left_sum << endl;
      } else {
        long long right_sum = 0;
        long long min_res = left_sum + right_sum;

        //cout << "if all to " << r << ", result is " << left_sum << " + " << right_sum << endl;

        long long right_cnt = divisor - left_cnt;
        right_sum += right_cnt;
        a_for_iteration[r] -= right_cnt;

        for (int j = r - 1; j >= l; --j) {
          left_sum -= left_cnt;
          left_cnt -= a_for_iteration[j];

          //cout << "if all to " << j << ", result is " << left_sum << " + " << right_sum << endl;
          min_res = min(min_res, left_sum + right_sum);

          right_cnt += a_for_iteration[j];
          right_sum += right_cnt;
        }
        cur_ans += min_res;
        l = r;
      }
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      for (int j = 0; j < n; ++j) {
        a_for_iteration[j] = a[j] % i;
        // cout << a_for_iteration[j] << " ";
      }
      // cout << endl;
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
long long pref[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
    pref[i] = sum;
  }

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

  auto try_for_divisor = [&](int divisor) {
    long long cur_ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      int rem = pref[i] % divisor;
      cur_ans += min(rem , divisor - rem);
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i * 1ll * i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      try_for_divisor(i);
    }
  }

  if (sum > 1) {
    try_for_divisor(sum);
  }
  cout << ans << "\n";
  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
int a_for_iteration[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  auto try_for_divisor = [&](int divisor) {
    // cout << "try for divisor: " << divisor << endl;
    long long cur_ans = 0;
    int l = 0;
    int r = 0;

    long long left_sum = 0;
    long long left_cnt = 0;

    while (l < n && r < n) {
      if (left_cnt + a_for_iteration[r] < divisor) {
        left_cnt += a_for_iteration[r++];
        left_sum += left_cnt;
      } else {
        long long right_sum = 0;
        long long min_res = left_sum + right_sum;

        long long right_cnt = divisor - left_cnt;
        right_sum += right_cnt;
        a_for_iteration[r] -= right_cnt;

        for (int j = r - 1; j >= l; --j) {
          left_sum -= left_cnt;
          left_cnt -= a_for_iteration[j];
          min_res = min(min_res, left_sum + right_sum);

          right_cnt += a_for_iteration[j];
          right_sum += right_cnt;
        }
        cur_ans += min_res;
        l = r;
      }
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      for (int j = 0; j < n; ++j) {
        a_for_iteration[j] = a[j] % i;
      }
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
long long pref[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
    pref[i] = sum;
  }

  auto try_for_divisor = [&](int divisor) {
    long long cur_ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      int rem = pref[i] % divisor;
      cur_ans += min(rem , divisor - rem);
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e5 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
int a_for_iteration[MAXN];

int main() {
  int n;
  int sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  auto try_for_divisor = [&](int divisor) {
    // cout << "try for divisor: " << divisor << endl;
    long long cur_ans = 0;
    int l = 0;
    int r = 0;

    long long left_sum = 0;
    long long left_cnt = 0;

    while (l < n && r < n) {
      if (left_cnt + a_for_iteration[r] < divisor) {
        left_cnt += a_for_iteration[r++];
        left_sum += left_cnt;
      } else {
        long long right_sum = 0;
        long long min_res = left_sum + right_sum;

        long long right_cnt = divisor - left_cnt;
        right_sum += right_cnt;
        a_for_iteration[r] -= right_cnt;

        for (int j = r - 1; j >= l; --j) {
          left_sum -= left_cnt;
          left_cnt -= a_for_iteration[j];
          min_res = min(min_res, left_sum + right_sum);

          right_cnt += a_for_iteration[j];
          right_sum += right_cnt;
        }
        cur_ans += min_res;
        l = r;
      }
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (int i = 2; i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      has_good_divisors = true;
      for (int j = 0; j < n; ++j) {
        a_for_iteration[j] = a[j] % i;
      }
      try_for_divisor(i);
    }
  }

  if (!has_good_divisors) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1000000000ll * 1000000000ll;
int a[MAXN];
long long pref[MAXN];

int main() {
  int n;
  long long sum = 0;
  long long ans = INF;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i];
    pref[i] = sum;
  }

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

  auto try_for_divisor = [&](long long divisor) {
    long long cur_ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      long long rem = pref[i] % divisor;
      cur_ans += min(rem , -rem + divisor);
    }
    ans = min(ans, cur_ans);
  };

  bool has_good_divisors = false;
  for (long long i = 2; i * i <= sum; ++i) {
    if (!(sum % i)) {
      while (!(sum % i)) {
        sum /= i;
      }
      try_for_divisor(i);
    }
  }

  if (sum > 1) {
    try_for_divisor(sum);
  }
  cout << ans << "\n";
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],b[MAXN],s,k;
ll solve(int l,int r){
    ll s=0,res=0;
    int j;
    for(j=l;j<=r;++j){
        s+=a[j];
        if(s>k/2)break;
    }
    for(int i=l;i<=r;++i)res+=a[i]*abs(i-j);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
        b[i]=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=inf;
    for(k=2;k<=s;++k){
        if(s%k)continue;
        ll ss=0,tmp_ans=0;
        int l=1;
        for(int i=1;i<=n;++i){
            ss+=a[i];
            while(ss>=k){
                ll tmp=ss-k;
                a[i]-=tmp;
                tmp_ans+=solve(l,i);
                l=i;
                ss=a[i]=tmp;
            }
        }
        for(int i=1;i<=n;++i)a[i]=b[i];
        ans=min(ans,tmp_ans );
    }
    printf("%I64d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],s;
ll solve(ll k){
    ll pre=0,res=0;
    for(int i=1;i<=n;++i)(pre+=a[i])%=k,res+=min(pre,k-pre);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=1ll<<63,k;
    for(k=2;k*k<=s;++k){
        if(s%k)continue;
        while(s%k==0)s/=k;
        ans=min(ans,solve(k));
    }
    if(k>1)ans=min(ans,solve(s));
    printf("%I64d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],b[MAXN],s,k;
ll solve(int l,int r){
    ll s=0,res=0;
    int j;
    for(j=l;j<=r;++j){
        s+=a[j];
        if(s>k/2)break;
    }
    for(int i=l;i<=r;++i)res+=a[i]*abs(i-j);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
        b[i]=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=inf;
    for(k=2;k<=s;++k){
        if(s%k)continue;
        while(s%k==0)s/=k;
        ll ss=0,tmp_ans=0;
        int l=1;
        for(int i=1;i<=n;++i){
            ss+=a[i];
            while(ss>=k){
                ll tmp=ss-k;
                a[i]-=tmp;
                tmp_ans+=solve(l,i);
                l=i;
                ss=a[i]=tmp;
            }
        }
        for(int i=1;i<=n;++i)a[i]=b[i];
        ans=min(ans,tmp_ans );
    }
    printf("%I64d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],s;
ll solve(ll k){
    ll pre=0,res=0;
    for(int i=1;i<=n;++i)(pre+=a[i])%=k,res+=min(pre,k-pre);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=1ll<<50,k;
    for(k=2;k*k<=s;++k){
        if(s%k)continue;
        while(s%k==0)s/=k;
        ans=min(ans,solve(k));
    }
    if(k>1)ans=min(ans,solve(s));
    printf("%I64d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],s;
ll solve(ll k){
    ll pre=0,res=0;
    for(int i=1;i<=n;++i)(pre+=a[i])%=k,res+=min(pre,k-pre);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=1ll<<62,k;
    for(k=2;k*k<=s;++k){
        if(s%k)continue;
        while(s%k==0)s/=k;
        ans=min(ans,solve(k));
    }
    if(s>1)ans=min(ans,solve(s));
    printf("%I64d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*P1=buf,*P2=buf;
#define gc() (P1==P2&&(P2=(P1=buf)+fread(buf,1,1<<20,stdin),P1==P2)?EOF:*P1++)
#define TT template<class T>inline
TT bool read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=x*10+c-48,c=gc();
    if(f)x=-x;return 1;
}
TT bool read(T&a,T&b){return read(a)&&read(b);}
TT bool read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
#define Init(a,v) memset(a,v,sizeof(a))
#define lowbit(x) (x&(-x))
const ll MAXN=1e6+8,mod=1e9+7,inf=0x3f3f3f3f;
ll n,a[MAXN],s;
ll solve(ll k){
    ll pre=0,res=0;
    for(int i=1;i<=n;++i)(pre+=a[i])%=k,res+=min(pre,k-pre);
    return res;
}
int main() {
    read(n);
    for(int i=1;i<=n;++i){
        read(a[i]);
        s+=a[i];
    }
    if(s<=1){printf("-1");return 0;}
    ll ans=1ll<<62,k;
    for(k=2;k*k<=s;++k){
        if(s%k)continue;
        while(s%k==0)s/=k;
        ans=min(ans,solve(k));
    }
    if(k>1)ans=min(ans,solve(s));
    printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006],tmp[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot;++j){
	    if(sum%prime[j]) continue;
		int top=0;ll tsum=0;
		for(int i=1;i<=n;++i){
			if(a[i]%prime[j]) tmp[++top]=a[i]%prime[j],tsum+=a[i]%prime[j];
		}
		sort(tmp+1,tmp+top+1);
		ll k=tsum/prime[j];
		for(int i=top;i>=top-k+1;--i) tsum-=tmp[i];
		ans=min(ans,tsum);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006];
ll s[1000006],t[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && (ll)prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2ll*k*s[k]-(ll)k*s[i]-2ll*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+(ll)a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2ll*k*s[k]-(ll)k*s[i]-2ll*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006];
ll s[1000006],t[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 1000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+6];
int prime[N+6],tot=0;
int n;
int a[N+6];
ll s[N+6],t[N+6];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && (ll)prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2*k*s[k]-k*s[i]-2*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%sum,t[i]=t[i-1]+(ll)a[i]%sum*i;
			if(s[i]>=sum){
				ll ss=sum,tt=t[i]-(s[i]-sum)*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2*k*s[k]-k*s[i]-2*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=sum;
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006];
ll s[1000006],t[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && (ll)prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006],tmp[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	if(!sum) ans=0;
	for(int j=1;j<=tot && prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		int top=0;
		ll tsum=0;
		for(int i=1;i<=n;++i){
			if(a[i]%prime[j]) tmp[++top]=a[i]%prime[j],tsum+=a[i]%prime[j];
		}
		sort(tmp+1,tmp+top+1);
		ll k=tsum/prime[j];
		for(int i=top;i>=top-k+1;--i) tsum-=tmp[i];
		ans=min(ans,tsum);
		if(!ans) break;
	}
	if(ans && sum>1){
		int top=0;
		ll tsum=0;
		for(int i=1;i<=n;++i){
			if(a[i]%sum) tmp[++top]=a[i]%sum,tsum+=a[i]%sum;
		}
		sort(tmp+1,tmp+top+1);
		ll k=tsum/sum;
		for(int i=top;i>=top-k+1;--i) tsum-=tmp[i];
		ans=min(ans,tsum);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006];
ll s[1000006],t[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
		if(!ans) break;
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 10000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+7];
int prime[N+7],tot=0;
int n;
int a[1000006];
ll s[1000006],t[1000006];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && (ll)prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+(ll)a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]-k*s[i]-2*t[k]+2*k*s[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
#define N 1000000
#define inf 1000000000000000000ll
using namespace std;
bool iscmp[N+6];
int prime[N+6],tot=0;
int n;
int a[N+6];
ll s[N+6],t[N+6];
int main(){
	for(int i=2;i<=N;++i){
		if(!iscmp[i]) prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=N;++j){
		    iscmp[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;++i){
	    scanf("%d",&a[i]);
		sum+=a[i];
	}
	ll ans=inf;
	for(int j=1;j<=tot && (ll)prime[j]*prime[j]<=sum;++j){
	    if(sum%prime[j]) continue;
	    while(sum%prime[j]==0) sum/=prime[j];
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%prime[j],t[i]=t[i-1]+(ll)a[i]%prime[j]*i;
			if(s[i]>=prime[j]){
				ll ss=prime[j],tt=t[i]-(s[i]-prime[j])*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2*k*s[k]-k*s[i]-2*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=prime[j];
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans && sum>1){
		int j=tot+1;
		prime[j]=sum;
		s[0]=t[0]=0;
		ll tmp=0;int pre=0;
		for(int i=1;i<=n;++i){
			s[i]=s[i-1]+a[i]%sum,t[i]=t[i-1]+(ll)a[i]%sum*i;
			if(s[i]>=sum){
				ll ss=sum,tt=t[i]-(s[i]-sum)*i;
				swap(ss,s[i]);swap(tt,t[i]);
				ll tmp2=inf;
			    for(int k=pre;k<=i;++k){
			    	tmp2=min(tmp2,t[i]+2*k*s[k]-k*s[i]-2*t[k]);
				}
				tmp+=tmp2;
				swap(ss,s[i]);swap(tt,t[i]);
			    pre=i;
				s[i]-=sum;
				t[i]=s[i]*i;
			}
		}
		ans=min(ans,tmp);
	}
	if(ans==inf) printf("-1");
	else printf("%I64d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10;

int a[N];
int n;

ll solve(ll x){
    ll ans = 0;
    ll sum = 0;
    for(int i = 1;i <= n;i++){
        sum = (sum+a[i])%x;
        ans += min(sum,x-sum);
    }
    return ans;
}
int main(){
  //  freopen("out.txt","r",stdin);
    ll sum = 0;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",a+i);
        sum += a[i];
    }
    if(sum == 0){
        printf("0\n");
        return 0;
    }else if(sum == 1){
        printf("-1\n");
        return 0;
    }
    ll ans = 0x7f7f7f7f7f7f7f7f;
    //���������
    for(ll i = 2;i*i <= sum;i++){
        if(sum%i == 0){
            ans = min(ans,solve(i));
            while(sum%i == 0) sum /= i;
        }
    }
    if(sum > 1) ans = min(ans,solve(sum));
    printf("%lld\n",ans);
    return 0;
}