#include <bits/stdc++.h>
using namespace std;
int a[1000010], n;
long long go(long long x) {
  long long s = 0, res = 0, c = 0;
  int mid = -1;
  for (int i = 0; i < n; i++) {
    long long r = a[i] + s;
    if (r >= (x + 1) / 2 && mid == -1) {
      c = 0;
      mid = i;
    }
    if (r >= x) {
      res += (long long)(i - mid) * (x - s);
      r -= x;
      mid = -1;
      s = c = r % x;
      if (s >= (x + 1) / 2 && mid == -1) {
        c = 0;
        mid = i;
      }
      res += c;
    } else if (mid == -1) {
      s = c = r;
      res += c;
    } else if (i >= mid) {
      s = r;
      res += (long long)(i - mid) * a[i];
    }
  }
  return res;
}
int main() {
  scanf("%d", &n);
  long long s = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    s += a[i];
  }
  if (s <= 1) {
    puts("-1");
    return 0;
  }
  vector<long long> p;
  for (int i = 2; i <= 1000000; i++) {
    if (i > s) break;
    if (s % i == 0) {
      p.push_back(i);
      while (s % i == 0) {
        s /= i;
      }
    }
  }
  if (s > 1) {
    p.push_back(s);
  }
  long long ans = 1ll << 60;
  for (int i = 0; i < p.size(); i++) {
    ans = min(ans, go(p[i]));
  }
  printf("%I64d\n", ans);
  return 0;
}
