#include <bits/stdc++.h>
using namespace std;
int n, a[1000006];
long long num, s[1000006];
long long solve(long long p) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += min(s[i] % p, p - s[i] % p);
  }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  num = s[n];
  if (num == 1) {
    cout << -1 << '\n';
    return 0;
  }
  long long ans = 1e18;
  for (long long p = 2; p * p <= num; p++) {
    if (num % p == 0) {
      ans = min(ans, solve(p));
      while (num % p == 0) num /= p;
    }
  }
  if (num > 1) ans = min(ans, solve(num));
  cout << ans << '\n';
  return 0;
}
