#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int base = 19260817;
const long long INF = 1e18;
const int maxn = 1e5 + 10;
const double eps = 1e-8;
inline int add(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}
inline int sub(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
inline int mul(int x, int y) { return 1LL * x * y % mod; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << -1;
    return 0;
  }
  long long ans = LLONG_MAX;
  auto calc = [=](long long d) -> long long {
    long long res = 0, now = 0;
    for (int i = 0; i < n - 1; i++) {
      now += a[i];
      res += min(now % d, d - now % d);
    }
    return res;
  };
  for (long long d = 2; d * d <= sum; d++)
    if (sum % d == 0) {
      ans = min(ans, calc(d));
      while (sum % d == 0) sum /= d;
    }
  if (sum > 1) ans = min(ans, calc(sum));
  cout << ans;
  return 0;
}
