#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long sum = 0;
  cin >> n;
  long long a[n + 5];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<long long> candidates;
  for (long long i = 2; i * i <= sum; i += 1) {
    if (sum % i == 0) {
      while (sum % i == 0) {
        sum /= i;
      }
      candidates.push_back(i);
    }
  }
  if (sum > 1) candidates.push_back(sum);
  long long ans = 2e18;
  for (auto it : candidates) {
    long long pre = 0, tot = 0;
    for (int i = 1; i <= n; i++) {
      pre += a[i];
      pre %= it;
      tot += min(pre, (it - pre) % it);
    }
    ans = min(ans, tot);
  }
  if (ans == 2e18)
    cout << "-1\n";
  else
    cout << ans << '\n';
  return 0;
}
