#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
long long int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 1; i < n; i++) {
    a[i] += a[i - 1];
  }
  if (a[n - 1] <= 1) {
    printf("-1\n");
    return 0;
  }
  long long int sm = a[n - 1];
  vector<long long int> prms;
  for (long long int i = 2; i * i <= sm; i++) {
    if (sm % i == 0) {
      prms.push_back(i);
      while (sm % i == 0) sm /= i;
    }
  }
  if (sm != 1) {
    prms.push_back(sm);
  }
  long long int ans = 1e18;
  for (auto x : prms) {
    long long int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += min(a[i] % x, x - a[i] % x);
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
