#include <bits/stdc++.h>
using namespace std;
int n;
int *a;
long long int *s;
long long int res = LLONG_MAX;
void solve(long long int N) {
  for (long long int i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      long long int tempres = 0;
      for (int j = 1; j <= n; j++) {
        tempres += min(s[j] % i, i - s[j] % i);
      }
      res = min(tempres, res);
      while (N % i == 0) {
        N /= i;
      }
    }
  }
  if (N > 1) {
    long long int tempres = 0;
    for (int j = 1; j <= n; j++) {
      tempres += min(s[j] % N, N - s[j] % N);
    }
    res = min(tempres, res);
  }
  printf("%lld", res);
}
int main() {
  scanf("%d", &n);
  s = new long long int[n + 1];
  a = new int[n + 1];
  s[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s[i] = a[i] + s[i - 1];
  }
  if (s[n] == 1) {
    printf("-1");
    return 0;
  }
  solve(s[n]);
  return 0;
}
