#include <bits/stdc++.h>
using namespace std;
long long solve(vector<int>& a, long long m) {
  long long ans = 0, excess = 0;
  for (int x : a) {
    if (excess <= m / 2) {
      ans += excess;
    } else {
      ans += (m - excess);
    }
    excess = (excess + x) % m;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long sum = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = (int)(0); i < (int)(n); i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << "-1\n";
    return 0;
  }
  long long ans = solve(a, sum);
  if (sum % 2 == 0) {
    ans = min(ans, solve(a, 2));
    while (sum % 2 == 0) sum /= 2;
  }
  int m = (int)sqrt(sum);
  for (int i = 3; i <= m; i += 2) {
    if (sum % i == 0) {
      ans = min(ans, solve(a, i));
      while (sum % i == 0) sum /= i;
    }
  }
  if (sum > 1) {
    ans = min(ans, solve(a, sum));
  }
  cout << ans << endl;
  return 0;
}
