#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
inline bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    if (i) A[i] += A[i - 1];
  }
  if (A[N - 1] <= 1) {
    cout << -1 << '\n';
    return 0;
  }
  long long ans = 1e18L;
  auto get = [&](long long p) {
    long long cost = 0;
    for (int i = 0; i < N; ++i) cost += min(A[i] % p, p - (A[i] % p));
    return cost;
  };
  long long p = 2, x = A[N - 1];
  while (p * p <= x) {
    if (x % p == 0) {
      uin(ans, get(p));
      while (x % p == 0) x /= p;
    }
    ++p;
  }
  if (x > 1) uin(ans, get(x));
  cout << ans << '\n';
}
