#include <bits/stdc++.h>
using namespace std;
long long arr[1000007];
long long sum = 0, mn = 1000000000000000007, num;
void operation(long long fact) {
  long long ans = 0;
  long long pFact = fact;
  for (int i = 1; i <= num; i++) {
    ans += min(arr[i] % pFact, pFact - (arr[i]) % pFact);
  }
  mn = min(mn, ans);
}
void primeFactors_AND_solve(long long n) {
  bool f1 = false;
  while (n % 2 == 0) {
    n = n / 2;
    f1 = true;
  }
  if (f1) operation(2);
  for (long long i = 3; i * i <= n; i = i + 2) {
    if (n % i) continue;
    bool f2 = false;
    while (n % i == 0) {
      n = n / i;
      f2 = true;
    }
    if (f2) operation(i);
  }
  if (n > 2) operation(n);
}
int main() {
  scanf("%lld", &num);
  for (int i = 1; i <= num; i++) {
    long long temp;
    scanf("%lld", &temp);
    sum += temp;
    arr[i] = arr[i - 1] + temp;
  }
  if (num == 1 && sum == 1 || sum == 1) {
    printf("-1\n");
    return 0;
  }
  primeFactors_AND_solve(sum);
  printf("%lld\n", mn);
  return 0;
}
