#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const long long INF = 1LL << 61;
vector<lint> prime_factors(lint a) {
  vector<lint> res;
  if (a % 2 == 0) {
    do a /= 2;
    while (a % 2 == 0);
    res.emplace_back(2);
  }
  for (lint p = 3; p * p <= a; p += 2)
    if (a % p == 0) {
      do a /= p;
      while (a % p == 0);
      res.emplace_back(p);
    }
  if (a > 1) res.emplace_back(a);
  sort(res.begin(), res.end());
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<lint> sum(n + 1);
  for (int i = 0; i < (n); i++) {
    int a;
    scanf("%d", &a);
    sum[i + 1] = sum[i] + a;
  }
  if (sum[n] == 1) return puts("-1"), 0;
  lint ans = INF;
  for (lint p : prime_factors(sum[n])) {
    lint cost = 0;
    for (int i = 0; i < (n - 1); i++)
      cost += min(sum[i + 1] % p, p - sum[i + 1] % p);
    ans = min(ans, cost);
  }
  printf("%lld\n", ans);
  return 0;
}
