#include <bits/stdc++.h>
using namespace std;
const int mex = 1e6 + 6;
long long int n, a[mex + 2], k, l, o;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int prime[mex + 2] = {0};
  vector<long long int> p;
  for (long long int i = 2; i <= mex; i++) {
    if (prime[i] == 0) {
      p.push_back(i);
      for (long long int j = i * i; j < mex; j += i) prime[j] = 1;
    }
  }
  cin >> n;
  k = 0;
  for (long long int i = 0; i <= n - 1; i++) cin >> a[i], k += a[i];
  if (k == 1) {
    cout << -1;
    exit(0);
  }
  vector<long long int> v;
  for (auto y : p) {
    if (k % y == 0) {
      v.push_back(y);
      while (k % y == 0) k /= y;
    }
  }
  if (k > 1) v.push_back(k);
  long long int ans = 1e18;
  for (long long int i = 1; i <= n - 1; i++) a[i] += a[i - 1];
  for (auto y : v) {
    long long int val = 0;
    for (long long int i = 0; i <= n - 1; i++)
      val += min(a[i] % y, y - (a[i] % y));
    ans = min(ans, val);
  }
  cout << ans;
  return 0;
}
