#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  long long s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  vector<long long> to_check;
  for (long long x = 2; x * x <= s; x++) {
    if (s % x == 0) {
      while (s % x == 0) {
        s /= x;
      }
      to_check.push_back(x);
    }
  }
  if (s != 1) {
    to_check.push_back(s);
  }
  if (to_check.empty()) {
    cout << -1 << '\n';
    return 0;
  }
  long long ans = 1e18;
  for (long long x : to_check) {
    long long cur = 0;
    long long tot = 0;
    for (int i = 0; i < n; i++) {
      cur += a[i] % x;
      cur %= x;
      tot += min(cur, x - cur);
    }
    ans = min(ans, tot);
  }
  cout << ans << endl;
}
