#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 1e6 + 6, MAXM = 2e5 + 5;
long long arr[MAXN];
void Solve() {
  long long N, sum = 0;
  cin >> N;
  for (long long i = 1; i <= N; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  if (sum == 1) {
    cout << -1;
    return;
  }
  vector<long long> pr;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      pr.push_back(i);
      while (sum % i == 0) {
        sum /= i;
      }
    }
  }
  if (sum > 1) pr.push_back(sum);
  long long ans = 1e18;
  for (auto x : pr) {
    long long tmp = 0, pref = 0;
    for (long long i = 1; i <= N; i++) {
      pref += arr[i];
      tmp += min(pref % x, x - pref % x);
    }
    ans = min(ans, tmp);
  }
  cout << ans;
}
signed main() {
  if (fopen(""
            ".inp",
            "r")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  };
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long TC = 1;
  while (TC--) Solve();
  return 0;
}
