#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
const double PI = acos(-1), ebs = 1e-8;
vector<vector<double> > Matrix_Zero(int n, int m) {
  return vector<vector<double> >(n, vector<double>(m, 0.0));
}
vector<vector<double> > Matrix_Ident(int n) {
  vector<vector<double> > Res = Matrix_Zero(n, n);
  for (int i = 0; i < n; i++) Res[i][i] = 1.0;
  return Res;
}
vector<vector<double> > Matrix_Multy(const vector<vector<double> >& A,
                                     const vector<vector<double> >& B) {
  assert(A[0].size() == B.size());
  vector<vector<double> > Res = Matrix_Zero(A.size(), B[0].size());
  for (int i = 0; i < A.size(); i++)
    for (int j = 0; j < B[0].size(); j++)
      for (int k = 0; k < A[0].size(); k++) Res[i][j] += A[i][k] * B[k][j];
  return Res;
}
vector<vector<double> > Matrix_Power(const vector<vector<double> >& A, int k) {
  if (!k) return Matrix_Ident(A.size());
  if (k & 1) return Matrix_Multy(A, Matrix_Power(A, k - 1));
  return Matrix_Power(Matrix_Multy(A, A), k >> 1);
}
string add(string s1, string s2) {
  reverse(s1.begin(), s1.end());
  reverse(s2.begin(), s2.end());
  string ss = "";
  int i = 0, j = 0, nxt = 0;
  while (i < s1.size() || j < s2.size() || nxt) {
    int temp = (i < s1.size() ? s1[i] - '0' : 0) +
               (j < s2.size() ? s2[j] - '0' : 0) + nxt;
    ss.push_back(temp % 10 + '0');
    nxt = temp / 10;
    i++, j++;
  }
  reverse(ss.begin(), ss.end());
  return ss;
}
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
bool cmp(const pair<int, string>& a, const pair<int, string>& b) {
  return (a.first > b.first);
}
long long int ceil(long long int x, long long int y) {
  return x % y ? x / y + 1 : x / y;
}
long long int power(long long int x, long long int y, long long int mo) {
  long long int r = 1;
  x = x % mo;
  while (y) {
    if (y & 1) r = (r * x % mo) % mo;
    y = y >> 1;
    x = ((x % mo) * (x % mo)) % mo;
  }
  return r % mo;
}
long long int gcd(long long int x, long long int y) {
  return y ? gcd(y, x % y) : x;
}
long long int fac(long long int x) { return (x ? x * fac(x - 1) : 1); }
long long int inv(long long int x, long long int y) {
  return (x * power(y, mod - 2, mod)) % mod;
}
long long int Twafiq(int n, int r) {
  long long int res = fac(n);
  long long int div = fac(n - r) * fac(r);
  div %= mod;
  div = power(div, mod - 2, mod);
  return (res * div) % mod;
}
void GO() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cout << fixed << setprecision(12);
}
long long int n, a[1000010], sum, ans = 1LL << 61, res, Mod;
vector<long long int> primes;
int main() {
  GO();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    sum += a[i];
  }
  for (long long int i = 2; i * i <= sum; i++) {
    if (sum % i == 0) primes.push_back(i);
    while (sum % i == 0) sum /= i;
  }
  if (sum != 1) primes.push_back(sum);
  for (long long int prime : primes) {
    Mod = res = 0;
    for (long long int i = 0; i < n; i++) {
      Mod = ((a[i] % prime) + Mod) % prime;
      res += min(Mod, prime - Mod);
    }
    ans = min(ans, res);
  }
  cout << (ans == 1LL << 61 ? -1LL : ans) << '\n';
  return 0;
}
