#include <bits/stdc++.h>
using namespace std;
inline int64_t max(int64_t a, int64_t b) { return (a > b) ? a : b; }
inline int64_t min(int64_t a, int64_t b) { return (a < b) ? a : b; }
inline int64_t max3(int64_t a, int64_t b, int64_t c) {
  return max(a, max(b, c));
}
inline int64_t min3(int64_t a, int64_t b, int64_t c) {
  return min(a, min(b, c));
}
int64_t gcd(int64_t a, int64_t b) { return (a % b == 0) ? b : gcd(b, a % b); }
int64_t pow3(int64_t a, int64_t b, int64_t m = 1000000007) {
  int64_t res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  while (q--) {
    int64_t n;
    cin >> n;
    int64_t a[n];
    for (int64_t i = 0; i < n; i++) cin >> a[i];
    int64_t sum[n];
    sum[0] = a[0];
    for (int64_t i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i];
    int64_t ans = 1e18;
    if (sum[n - 1] == 1) {
      cout << -1;
      return 0;
    }
    int64_t temp = sum[n - 1];
    int64_t p = 2;
    while (p * p <= temp) {
      if (temp % p == 0) {
        int64_t tans = 0;
        for (int64_t i = 0; i < n; i++) tans += min(sum[i] % p, p - sum[i] % p);
        ans = min(ans, tans);
        while (temp % p == 0) temp /= p;
      }
      if (p == 2)
        p++;
      else
        p += 2;
    }
    if (temp > 1) {
      int64_t tans = 0;
      for (int64_t i = 0; i < n; i++)
        tans += min(sum[i] % temp, temp - sum[i] % temp);
      ans = min(ans, tans);
    }
    cout << ans;
  }
  return 0;
}
