#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const long long inf = 9223372036854775807LL;
long long s[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    long long x;
    scanf("%lld", &x);
    s[i + 1] = x + s[i];
  }
  long long sum = s[n];
  vector<long long> v;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) v.push_back(i);
    while (sum % i == 0) sum /= i;
  }
  if (sum > 1) v.push_back(sum);
  if (!v.size()) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = inf;
  for (int i = 0; i < v.size(); i++) {
    long long u = v[i], tmp = 0;
    for (int i = 1; i <= n - 1; i++) tmp += min(s[i] % u, u - s[i] % u);
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
