#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
long long n;
long long a[N];
long long sovle(long long p) {
  long long res = 0, x = 0;
  for (long long i = 0; i < n; i++) {
    x = (a[i] + x) % p;
    res += min(x, p - x);
  }
  return res;
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum <= 1) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 125193768297041258 * 3;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      while (sum % i == 0) sum /= i;
      ans = min(ans, sovle(i));
    }
  }
  if (sum != 1) ans = min(ans, sovle(sum));
  cout << ans << endl;
  return 0;
}
