#include <bits/stdc++.h>
using namespace std;
long long s[1000010], a[1000010];
int n;
long long count(long long div) {
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += min(s[i] % div, div - s[i] % div);
  }
  return ans;
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  cin >> s[0];
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  long long sum = s[n - 1];
  if (sum == 1) {
    cout << -1;
    return 0;
  }
  long long ans = 1e18;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      ans = min(ans, count(i));
      while (sum % i == 0) sum /= i;
    }
  }
  if (sum > 1) ans = min(ans, count(sum));
  cout << ans;
}
