#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long n, sum[10000005];
bool get(long long x, long long y) { return (x >> y) & 1; }
long long calc(long long k) {
  long long res = 0;
  for (long long i = 1; i <= n; i++)
    res = res + min(sum[i] % k, k - sum[i] % k);
  return res;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    sum[i] = sum[i - 1] + x;
  }
  long long total = sum[n];
  long long ans = inf;
  for (long long k = 2; k <= sqrt(total); k++) {
    if (total % k == 0) {
      ans = min(ans, calc(k));
      while (total % k == 0) total /= k;
    }
  }
  if (total > 1) ans = min(ans, calc(total));
  if (ans == inf) ans = -1;
  cout << ans;
  return 0;
}
