#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], n;
long long ans = 1e18, b[N];
void cal(long long x) {
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
  }
  long long sum1 = 0, sum2 = 0, mn = 0, pos = 0;
  for (long long i = 1; i <= n;) {
    if (sum1 + b[i] <= x) {
      sum1 += b[i];
      sum2 += b[i] * i;
      if (pos) {
        mn += b[i] * (i - pos);
      } else if (sum1 >= x / 2 + x % 2) {
        pos = i;
        mn += sum1 * i - sum2;
      }
      i++;
    } else if (sum1 + b[i] > x) {
      if (pos) {
        mn += (x - sum1) * (i - pos);
      } else {
        mn += sum1 * i - sum2;
      }
      b[i] -= x - sum1;
      sum1 = 0;
      sum2 = 0;
      pos = 0;
    }
  }
  ans = min(ans, mn);
}
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  if (sum == 1) {
    printf("-1");
    return 0;
  }
  for (int i = 2; i <= sqrt(sum); i++) {
    if (sum % i == 0) {
      cal(i);
      cal(sum / i);
    }
  }
  cal(sum);
  printf("%lld", ans);
}
