#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long N;
long long sum = 0, ret;
void test(long long val) {
  long long toAdd = 0;
  long long retc = 0;
  for (int i = 0; i < ((int)(v).size()); ++i) {
    long long cur = v[i] + toAdd;
    if (cur < 0) {
      cur += val;
    }
    if (cur >= val) cur -= val;
    long long adx = cur % val;
    long long rm = (val - (cur % val)) % val;
    if (adx < rm) {
      retc += adx;
      toAdd = adx;
    } else {
      retc += rm;
      toAdd = -rm;
    }
  }
  ret = min(ret, retc);
}
int main() {
  cin.sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < (N); ++i) {
    int x;
    cin >> x;
    v.push_back(x);
    sum += x;
  }
  ret = sum * N;
  if (sum == 1) {
    cout << -1;
    return 0;
  }
  for (long long i = 2; i * i <= sum; ++i) {
    if (sum % i == 0) {
      test(i);
      while (sum % i == 0) sum = sum / i;
    }
  }
  if (sum > 1) test(sum);
  cout << ret;
}
