#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long n, m;
long long a[10000005];
long long s[10000005];
long long cost(long long p) {
  long long final = 0;
  for (long long i = 1; i <= n; i++) {
    final += min(s[i] % p, p - (s[i]) % p);
  }
  return final;
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  s[0] = 0;
  for (long long i = 1; i <= n; i++) {
    s[i] = s[i - 1] + a[i - 1];
  }
  if (s[n] == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long tmp = s[n];
  long long p = 2;
  long long ans = INT64_MAX;
  while (p * p <= tmp) {
    if (tmp % p == 0) {
      ans = min(ans, cost(p));
      while (tmp % p == 0) tmp /= p;
    }
    p++;
  }
  if (tmp > 1) ans = min(ans, cost(tmp));
  cout << ans << endl;
}
