#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const long long MOD2 = 998244353;
const long long N = 1000 * 100 + 5;
const long long N2 = 1000 * 1000 * 10;
const long double PI = 3.14159265;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long power(long long x, long long y, long long p = INF) {
  long long res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long calc(long long num, long long n, long long a[]) {
  long long res = 0;
  long long sum = 0;
  for (long long i = 0; i < n; ++i) {
    sum += a[i];
    sum %= num;
    res += min(sum, num - sum);
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n];
  long long tot = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    tot += a[i];
  }
  if (tot == 1) {
    cout << -1;
    return 0;
  }
  std::vector<long long> prime;
  for (long long i = 2; i * i <= tot; i++) {
    if (!(tot % i)) {
      prime.push_back(i);
      while (!(tot % i)) {
        tot /= i;
      }
    }
  }
  if (tot > 1) prime.push_back(tot);
  long long ans = INF;
  for (auto x : prime) {
    ans = min(ans, calc(x, n, a));
  }
  cout << ans;
  return 0;
}
