#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  long long s[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    s[i] = sum;
  }
  if (sum == 1) {
    cout << -1 << "\n";
    return 0;
  }
  vector<long long> primes;
  if (sum % 2 == 0) {
    primes.push_back(2);
    while (sum % 2 == 0) {
      sum /= 2;
    }
  }
  for (long long i = 3; i <= sqrt(sum); i += 2) {
    if (sum % i == 0) {
      primes.push_back(i);
      while (sum % i == 0) {
        sum /= i;
      }
    }
  }
  if (sum > 2) {
    primes.push_back(sum);
  }
  long long res = LONG_LONG_MAX;
  for (long long i = 0; i < primes.size(); i++) {
    long long ans = 0;
    for (long long j = 0; j < n; j++) {
      ans += min(s[j] % primes[i], primes[i] - s[j] % primes[i]);
    }
    res = min(res, ans);
  }
  cout << res << "\n";
}
