#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto& x : a) cin >> x;
  vector<ll> ps;
  {
    ll sum = accumulate(a.begin(), a.end(), 0LL);
    for (ll x = 2; x * x <= sum; ++x) {
      if (sum % x == 0) {
        ps.push_back(x);
        while (sum % x == 0) sum /= x;
      }
    }
    if (sum > 1) ps.push_back(sum);
  }
  ll ans = inf;
  for (auto const p : ps) {
    ll carry = 0, tans = 0;
    for (int i = 0; i < n; ++i) {
      tans += abs(carry);
      ll cur = (a[i] + carry + p) % p;
      if (cur <= p / 2) {
        carry = cur;
      } else {
        carry = cur - p;
      }
    }
    ans = min(ans, tans);
    assert(carry == 0);
  }
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
