#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
long long a[N], res = -1;
int n;
void work(long long k) {
  long long tmp = 0;
  for (int i = 1; i < n; ++i) tmp += min(a[i] % k, k - a[i] % k);
  if (res == -1)
    res = tmp;
  else
    res = min(res, tmp);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), a[i] += a[i - 1];
  for (long long i = 2; i <= a[n] / i; ++i) {
    if (a[n] % i == 0) {
      work(i);
      while (a[n] % i == 0) a[n] /= i;
    }
  }
  if (a[n] != 1) work(a[n]);
  printf("%lld", res);
  return 0;
}
