#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long p[1000005];
long long res = 0x3f3f3f3f3f3f3f3f;
int n;
void check(long long k) {
  long long t = 0;
  for (int i = 1; i <= n; ++i) {
    long long q = p[i] % k;
    t += min(q, k - q);
  }
  res = min(res, t);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    p[i] += p[i - 1];
  }
  long long s = p[n];
  if (s == 1) {
    cout << -1;
    return 0;
  }
  for (long long i = 2; i * i <= s; ++i) {
    if (s % i == 0) check(i);
    while (s % i == 0) s /= i;
  }
  if (s != 1) check(s);
  cout << res;
}
