#include <bits/stdc++.h>
using namespace std;
const int DIM = 1000005;
long long arr[DIM], aux[DIM];
long long add(int &p, long long nr) {
  long long ans = 0;
  while (true) {
    if (!nr) break;
    if (nr <= aux[p]) {
      ans += nr * p;
      aux[p] -= nr;
      nr = 0;
      break;
    }
    ans += aux[p] * p;
    nr -= aux[p];
    aux[p] = 0;
    ++p;
  }
  return ans;
}
long long solve(long long v, int n) {
  long long ans = 0;
  for (int i = 1; i <= n; ++i) aux[i] = arr[i];
  int p = 1;
  long long md = (v + 1) / 2;
  while (true) {
    aux[p] %= v;
    while (p <= n and aux[p] == 0) ++p;
    if (p > n) break;
    long long s1 = add(p, md);
    if (v & 1) {
      s1 -= p;
    }
    long long s2 = add(p, v - md);
    ans += s2 - s1;
  }
  return ans;
}
int main(void) {
  int n;
  scanf("%d", &n);
  long long s = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &arr[i]);
    s += arr[i];
  }
  if (s < 2) {
    cout << -1;
    return 0;
  }
  long long ans = 1LL << 60;
  for (int i = 2; 1LL * i * i <= s; ++i) {
    if (s % i) continue;
    ans = min(ans, solve(i, n));
    while (s % i == 0) s /= i;
  }
  if (s > 1) ans = min(ans, solve(s, n));
  cout << ans << endl;
  return 0;
}
