#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
int n, a[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long sm = 0;
  for (int i = 1; i <= n; i++) {
    sm += a[i];
  }
  vector<long long> primes;
  for (long long i = 2; i * i <= sm; i++) {
    if (sm % i == 0) {
      primes.push_back(i);
      while (sm % i == 0) {
        sm /= i;
      }
    }
  }
  if (sm > 1) primes.push_back(sm);
  long long ans = 3e18;
  for (auto k : primes) {
    long long tot = 0, s = 0;
    for (int i = 1; i < n; i++) {
      s += a[i];
      s %= k;
      tot += min(s, k - s);
    }
    ans = min(ans, tot);
  }
  if (ans > 2e18) {
    cout << -1;
  } else {
    cout << ans;
  }
  return 0;
}
