#include <bits/stdc++.h>
using namespace std;
int read(int &x) { return scanf("%d", &x); }
int read(int &x, int &y) { return scanf("%d%d", &x, &y); }
int read(int &x, int &y, int &z) { return scanf("%d%d%d", &x, &y, &z); }
int read(long long &x) { return scanf("%lld", &x); }
int read(long long &x, long long &y) { return scanf("%lld%lld", &x, &y); }
int read(double &x) { return scanf("%lf", &x); }
char buff[2000010];
int read(string &s) {
  int r = scanf("%s", buff);
  s = buff;
  return r;
}
string NAME = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
long long solve(vector<int> &V, int l, int r, int c) {
  int m = c / 2;
  int mp = -1;
  long long ans = 0;
  for (int i = l; i <= r; ++i) {
    if (V[i] > m) {
      mp = i;
      break;
    }
    m -= V[i];
  }
  for (int i = l; i <= r; ++i) {
    ans += abs(i - mp) * V[i];
  }
  return ans;
}
long long getCost(vector<int> &V, long long x) {
  int st = -1;
  long long tot = 0;
  long long mv = x / 2;
  vector<long long> P(V.size());
  for (int i = 0; i < V.size(); ++i) {
    P[i] = V[i];
    if (i > 0) {
      P[i] += P[i - 1];
    }
  }
  for (int i = 0; i + 1 < V.size(); ++i) {
    long long ml = P[i] % x;
    tot += min(ml, x - ml);
  }
  return tot;
}
struct LinearSieve {
  vector<int> primes, primeFactor;
  LinearSieve(int L) : primeFactor(L + 1) {
    for (int i = 2; i < primeFactor.size(); ++i) {
      if (primeFactor[i] == 0) primes.push_back(primeFactor[i] = i);
      for (int j = 0; j < primes.size() && primes[j] <= primeFactor[i] &&
                      i * primes[j] <= L;
           ++j)
        primeFactor[i * primes[j]] = primes[j];
    }
  }
};
vector<long long> divisors(long long N) {
  LinearSieve LS(1000005);
  vector<long long> D;
  for (int p : LS.primes) {
    if (N % p == 0) {
      D.push_back(p);
      while (N % p == 0) N /= p;
    }
  }
  if (N != 1) {
    D.push_back(N);
  }
  return D;
}
int main() {
  int N;
  read(N);
  vector<int> V(N);
  int po = -1;
  int tot = 0;
  for (int i = 0; i < N; ++i) {
    read(V[i]);
  }
  long long sm = accumulate(V.begin(), V.end(), 0LL);
  if (sm == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long mn = 1000000000000000000LL;
  for (long long d : divisors(sm)) {
    mn = min(mn, getCost(V, d));
  }
  cout << mn << endl;
}
