#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int(i) = 0; (i) < (n); ++(i)) cin >> a[i];
  for (int(i) = 0; (i) < (n - 1); ++(i)) a[i + 1] += a[i];
  vector<ll> ps;
  {
    ll x = a.back();
    for (ll i = 2; i * i <= x; ++i) {
      if (x % i) continue;
      ps.push_back(i);
      while (x % i == 0) x /= i;
    }
    if (x > 1) ps.push_back(x);
  };
  if (ps.empty()) {
    cout << -1 << endl;
    return 0;
  }
  ll re = 1e18;
  for (ll p : ps) {
    ll s = 0;
    for (int(i) = 0; (i) < (n - 1); ++(i)) {
      auto x = a[i] % p;
      s += min(x, p - x);
    };
    MN(re, s);
  }
  cout << re << endl;
  return 0;
}
