#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long sum = 0;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  vector<long long> prime;
  for (long long i = 2; i * i <= sum; ++i) {
    if (sum % i == 0) {
      prime.push_back(i);
      while (sum % i == 0) sum /= i;
    }
  }
  if (sum > 1) prime.push_back(sum);
  long long ans = LLONG_MAX;
  for (long long i : prime) {
    long long now = 0;
    long long cnt = 0;
    for (long long j = 0; j < n; ++j) {
      cnt += a[j];
      now += min(cnt % i, i - cnt % i);
    }
    ans = min(ans, now);
  }
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
  return 0;
}
