#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
const long double error = 1e-7;
const double PI = acos(-1);
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const long long int infl = 1e18;
const int nmax = 105 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  vector<long long int> input(n);
  for (int i = 0; i < n; i++) cin >> input[i];
  long long int sum = accumulate(input.begin(), input.end(), 0ll);
  if (sum == 1) {
    cout << -1 << "\n";
    return 0;
  }
  vector<long long int> primes;
  for (long long int i = 2; i * i <= sum; i++) {
    if (sum % i != 0) continue;
    primes.push_back(i);
    while (sum % i == 0) sum /= i;
  }
  if (sum > 1) primes.push_back(sum);
  long long int ans = infl;
  for (int i = 0; i < primes.size(); i++) {
    long long int p = primes[i];
    vector<long long int> tmp(input);
    for (int j = 0; j < n; j++) tmp[j] %= p;
    long long int last = 0, carry = 0;
    long long int cand = 0;
    for (int j = 0; j < n; j++) {
      if (tmp[j] == 0) continue;
      tmp[j] = MOD(tmp[j] + carry, p);
      cand += abs(carry) * (j - last);
      if (tmp[j] > p - tmp[j]) {
        carry = tmp[j] - p;
      } else {
        carry = tmp[j];
      }
      last = j;
    }
    ans = min(ans, cand);
  }
  if (ans == infl) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
  return 0;
}
