#include <bits/stdc++.h>
using namespace std;
void MaxMercury() {
  if (0) {
    freopen(
        "taskA"
        ".in",
        "r", stdin);
    freopen(
        "taskA"
        ".out",
        "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
namespace MATH_CAL {
const int mod = (int)1e9 + 7;
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (long long)a * b % mod; }
inline void Add(int &a, int b) { (a += b) >= mod ? a -= mod : 0; }
inline void Sub(int &a, int b) { (a -= b) < 0 ? a += mod : 0; }
inline int qpow(int x, int n) {
  int r = 1;
  for (; n; n /= 2, x = mul(x, x))
    if (n & 1) r = mul(r, x);
  return r;
}
inline int mod_inv(int x) { return qpow(x, mod - 2); }
}  // namespace MATH_CAL
using namespace MATH_CAL;
const int maxN = (int)1e6 + 123;
const int MOD = (int)1e9 + 7;
const int INF = 1e9 + 12345;
const int CYCLE = -INF;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
unsigned long long a[maxN];
int n;
unsigned long long getResult(unsigned long long prime) {
  unsigned long long rem = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    rem += a[i];
    rem %= prime;
    ans += min(rem, prime - rem);
  }
  return ans;
}
void solve() {
  cin >> n;
  unsigned long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << -1 << endl;
    return;
  }
  auto save = sum;
  vector<unsigned long long> divs;
  for (unsigned long long i = 2; i * 1ll * i <= sum; i++) {
    if (sum % i == 0) {
      while (sum % i == 0) {
        sum /= i;
      }
      divs.push_back(i);
    }
  }
  if (sum > 1) {
    divs.push_back(sum);
  }
  unsigned long long ans = 0;
  unsigned long long res = 1e18;
  for (auto &prime : divs) {
    ans = getResult(prime);
    res = min(res, ans);
  }
  cout << res << endl;
}
int main() {
  MaxMercury();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
