#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 20;
int a[maxn];
vector<long long> get(long long n) {
  vector<long long> ans;
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      ans.push_back(i);
    }
  if (n > 1) ans.push_back(n);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) cin >> a[i], sum += a[i];
  if (sum == 1) return cout << -1 << endl, 0;
  auto primes = get(sum);
  long long ans = 1e18;
  for (auto p : primes) {
    long long s = 0, res = 0;
    for (int i = 0; i < n; i++) {
      s += a[i];
      res += min(p - (s % p), s % p);
    }
    ans = min(ans, res);
  }
  cout << ans << endl;
}
