#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("sse4.2")
long long gcd(long long n, long long m) {
  if (n == 0) return m;
  return gcd(m % n, n);
}
bool ifprime(long long n) {
  if (n == 1) return false;
  long long i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
void disp(vector<long long> v) {
  for (auto u : v) cout << u << " ";
  cout << "\n";
}
void remleadzero(vector<int> &v) {
  vector<int> u;
  int i = 0;
  while (v[i] == 0) i++;
  while (i < v.size()) {
    u.push_back(v[i]);
    i++;
  }
  v = u;
}
long long fast_expo(long long n, long long m, long long md) {
  long long a = 1;
  while (m > 0) {
    if (m & 1) a = (a * n) % md;
    n = (n * n) % md;
    m /= 2;
  }
  return a % md;
}
inline long long sqroot(long long n) {
  long double N = n;
  N = sqrtl(N);
  long long sq = N - 2;
  sq = max(sq, 0LL);
  while (sq * sq < n) {
    sq++;
  }
  if ((sq * sq) == n) return sq;
  return -1;
}
inline long long sqroot1(long long n) {
  long double N = n;
  N = sqrtl(N);
  long long sq = N - 2;
  sq = max(sq, 0LL);
  while (sq * sq < n) {
    sq++;
  }
  if ((sq * sq) == n) return sq;
  return sq - 1;
}
inline long long cbroot(long long n) {
  long double N = n;
  N = cbrtl(N);
  long long cb = N - 2;
  cb = max(cb, 0LL);
  while (cb * cb * cb < n) {
    cb++;
  }
  if ((cb * cb * cb) == n) return cb;
  return -1;
}
inline long long cbroot1(long long n) {
  long double N = n;
  N = cbrtl(N);
  long long cb = N - 2;
  cb = max(cb, 0LL);
  while (cb * cb * cb < n) {
    cb++;
  }
  if ((cb * cb * cb) == n) return cb;
  return cb - 1;
}
int dig_sum(long long n) {
  int ret = 0;
  while (n > 0) {
    ret += (n % 10);
    n /= 10;
  }
  return ret;
}
int sq(int x) { return x * x; }
long long dec(string s) {
  long long ret = 0;
  for (auto u : s) {
    ret <<= 1;
    ret += (u - '0');
  }
  return ret;
}
int gcd(int n, int m) {
  if (n == 0) return m;
  return gcd(m % n, n);
}
string update(string s) {
  int n = s.size();
  if (n < 2) return s;
  string ret = "";
  for (int i = 0; i < n; i++) {
    if (i < n - 1 and s[i] == ')' and s[i + 1] == '(') {
      i++;
      continue;
    }
    ret += s[i];
  }
  return ret;
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_num(long long n) {
  long long random_number = uniform_int_distribution<long long>(0, n - 1)(rng);
  return random_number;
}
chrono::high_resolution_clock::time_point start;
chrono::high_resolution_clock::time_point cur_time;
bool iscomp[1000006];
int lpf[1000006];
void solve_case() {
  chrono::high_resolution_clock::time_point cur_start =
      chrono::high_resolution_clock::now();
  long long sum = 0;
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    a[i] = sum;
  }
  vector<long long> prime;
  for (int i = 2; i <= 1e6; i++) {
    if (!iscomp[i]) {
      if (sum % i == 0) prime.push_back(i);
      while (sum % i == 0) {
        sum /= i;
      }
      if (sum == 1) break;
    }
  }
  if (sum != 1) {
    prime.push_back(sum);
  }
  long long ans = 1e18;
  for (auto u : prime) {
    long long cur = 0;
    for (int i = 0; i < n; i++) {
      cur += min(a[i] % u, u - a[i] % u);
    }
    ans = min(ans, cur);
  }
  if (ans == 1e18) ans = -1;
  cout << ans << '\n';
}
void precompute() {
  for (int i = 2; i <= 1e6; i++) {
    if (!iscomp[i]) {
      lpf[i] = i;
      for (int j = 2 * i; j <= 1e6; j += i) {
        iscomp[j] = true;
        if (!lpf[j]) lpf[j] = i;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  chrono::high_resolution_clock::time_point start =
      chrono::high_resolution_clock::now();
  precompute();
  int T = 1;
  cout << fixed << setprecision(10);
  for (int t = 1; t <= T; t++) {
    solve_case();
  }
  chrono::high_resolution_clock::time_point end =
      chrono::high_resolution_clock::now();
  long double time_spent =
      (chrono::duration_cast<chrono::duration<long double>>(end - start))
          .count();
  return 0;
}
