#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1000010;
int v[NMAX], n;
long long verif(long long s) {
  long long ans = 0, sact = 0;
  for (int i = 1; i < n; i++) {
    sact += v[i];
    ans += min(sact % s, s - sact % s);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    s += v[i];
  }
  long long best = 1e18;
  for (int i = 2; 1LL * i * i <= s; i++) {
    if (s % i == 0) best = min(best, verif(i));
    while (s % i == 0) s /= i;
  }
  if (s != 1) best = min(best, verif(s));
  if (best == 1e18)
    cout << "-1\n";
  else
    cout << best << '\n';
  return 0;
}
