#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<long long>;
using vpii = vector<pair<int, int>>;
using vvb = vector<vector<bool>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
using vvpii = vector<vector<pair<int, int>>>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using seti = set<int>;
using useti = unordered_set<int>;
vector<long long> factorize(long long x) {
  vector<long long> primes;
  for (int i = 2; 1LL * i * i <= x; i++)
    if (x % i == 0) {
      primes.push_back(i);
      while (x % i == 0) x /= i;
    }
  if (x > 1) primes.push_back(x);
  return primes;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  long long total = 0;
  long long tt;
  vl a(n);
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    total += a[i];
  }
  if (total == 1LL) {
    cout << -1 << "\n";
    return 0;
  }
  vl divisors = factorize(total);
  long long res = INT64_MAX;
  for (long long &divisor : divisors) {
    long long cost = 0;
    long long propagated = 0;
    for (int i = 0; i < (n - 1); i++) {
      long long now = a[i] + propagated;
      if (now > 0) {
        long long extra = now % divisor;
        if (extra < divisor - extra) {
          propagated = extra;
          cost += extra;
        } else {
          propagated = -divisor + extra;
          cost -= propagated;
        }
      } else {
        cost -= now;
        propagated = now;
      }
    }
    res = min(res, cost);
  }
  cout << res << "\n";
}
