#include <bits/stdc++.h>
using namespace std;
vector<long long> get_primes(long long n) {
  vector<long long> ans;
  for (long long d = 2; d * d <= n; d++) {
    bool is = 0;
    while (n % d == 0) {
      n /= d;
      is = 1;
    }
    if (is) {
      ans.push_back(d);
    }
  }
  if (n > 1) {
    ans.push_back(n);
  }
  return ans;
}
long long test(vector<long long> a, long long k) {
  int n = (int)a.size();
  for (int i = 1; i < n; i++) {
    a[i] += a[i - 1];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += min(a[i] % k, k - a[i] % k);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long sum = 0;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << "-1\n";
    return 0;
  }
  long long ans = (long long)1e18;
  vector<long long> t = get_primes(sum);
  for (auto &x : t) {
    ans = min(ans, test(a, x));
  }
  cout << ans << "\n";
}
