#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool mod(long double a, long double b) { return a / b - floor(a / b); }
long long occurs(vector<long long> v, long long n) {
  auto it = lower_bound(((v)).begin(), ((v)).end(), (n));
  auto it1 = upper_bound(((v)).begin(), ((v)).end(), (n));
  long long first = it1 - it;
  return first;
}
long long logb(long long base, long long first) {
  return (log(first) / log(base));
}
void set_min(long long &mn, long long k) {
  if (mn == -1 || mn > k) mn = k;
}
bool prime[1000005];
vector<long long> primes;
void SieveOfEratosthenes(long long n) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = 0;
  for (long long p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      if (n % p == 0) primes.push_back(p);
      for (long long i = p * p; i <= 1000003; i += p) prime[i] = false;
    }
  }
}
void prime_factors(long long n) {
  SieveOfEratosthenes(n);
  long long second = n, first, i;
  for (i = 0; i < primes.size(); i++) {
    first = primes[i];
    while (second % first == 0) {
      second /= first;
    }
  }
  if (second != 1) primes.push_back(second);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, first, n, m, t = 0, k, a = 0;
  cin >> n;
  vector<pair<long long, long long> > index;
  for (i = 0; i < n; i++) {
    cin >> first;
    if (first > 0) {
      t += first;
      index.push_back({i, first});
      a++;
    }
  }
  prime_factors(t);
  if (t == 1) {
    cout << -1;
    return 0;
  }
  long long mn = -1, l = index.size();
  vector<long long> fake;
  for (j = 0; j < l; j++) fake.push_back(index[j].second);
  for (i = 0; i < primes.size(); i++) {
    j = 0;
    long long fact = primes[i], start = 0, end = -1, mid = start + mid / 2,
              sum = 0, e = 0;
    while (j < l) {
      if (end == -1 || e == fact) {
        e = 0;
        start = j;
        long long tsum = 0;
        for (k = j; k < l; k++) {
          tsum += index[k].second;
          if (tsum >= (fact + 1) / 2) break;
        }
        mid = k;
        end = j + fact - 1;
      }
      if (e + index[j].second <= fact) {
        e += index[j].second;
        sum += (abs(index[mid].first - index[j].first) * index[j].second);
        j++;
      } else {
        end = -1;
        first = fact - e;
        index[j].second -= first;
        sum += abs((index[mid].first - index[j].first) * first);
        index[j].second %= fact;
      }
    }
    set_min(mn, sum);
    for (j = 0; j < l; j++) index[j].second = fake[j];
  }
  cout << mn;
  return 0;
}
