#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000005;
const long long inf = (long long)1 << 62;
vector<bool> prime;
void sieve() {
  prime.resize(MAX, true);
  prime[0] = false;
  prime[1] = false;
  for (long long i = 2; i < MAX; i++) {
    if (prime[i]) {
      for (long long j = i * 2; j < MAX; j += i) {
        prime[j] = false;
      }
    }
  }
}
vector<long long> getfactors(long long n) {
  set<long long> factors;
  for (long long i = 2; i < MAX; i++) {
    if (prime[i]) {
      while (n % i == 0) {
        n /= i;
        factors.insert(i);
      }
    }
  }
  if (n > 1) {
    factors.insert(n);
  }
  vector<long long> ret;
  for (auto i : factors) {
    ret.push_back(i);
  }
  return ret;
}
long long getmoves(vector<long long> v) {
  long long bestmoves = inf;
  long long moves = 0;
  for (long long i = 0; i < v.size(); i++) {
    moves += i * v[i];
  }
  long long l = 0;
  long long r = 0;
  for (long long i = 0; i < v.size(); i++) {
    r += v[i];
  }
  bestmoves = min(bestmoves, moves);
  for (long long i = 0; i < v.size(); i++) {
    l += v[i];
    r -= v[i];
    moves -= r;
    moves += l;
    bestmoves = min(bestmoves, moves);
  }
  return bestmoves;
}
long long solve(vector<long long> v, long long n) {
  for (auto& i : v) {
    i %= n;
  }
  long long moves = 0;
  long long tot = 0;
  vector<long long> curr;
  for (auto have : v) {
    if (tot + have >= n) {
      long long need = n - tot;
      curr.push_back(need);
      have -= need;
      moves += getmoves(curr);
      curr.clear();
      tot = 0;
    }
    tot += have;
    curr.push_back(have);
  }
  return moves;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  sieve();
  long long n;
  cin >> n;
  long long sum = 0;
  vector<long long> v(n);
  for (auto& i : v) {
    cin >> i;
    sum += i;
  }
  if (sum == 1) {
    cout << -1 << '\n';
    return 0;
  }
  vector<long long> factors = getfactors(sum);
  long long ans = inf;
  for (auto i : factors) {
    ans = min(ans, solve(v, i));
  }
  cout << ans << '\n';
}
