#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long N, A[101010], As;
long long mk[101010];
long long i, j, k, ans;
long long solve(long long a) {
  long long i = 0;
  long long ans = 0;
  while (i < N) {
    long long b = 0;
    j = i;
    while (i < N && b < a) {
      if (A[i]) {
        mk[b] = i;
      }
      b += A[i];
      i++;
    }
    if (i == N && b < a) break;
    long long c = mk[a / 2];
    if (a % 2 == 0) {
      c = (mk[a / 2 - 1] + mk[a / 2]) / 2;
    }
    for (j = 0; j < a; j++) {
      ans += abs(mk[j] - c);
    }
  }
  return ans;
}
int main() {
  scanf("%lld", &N);
  for (i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    As += A[i];
  }
  if (As == 1) {
    printf("-1");
    return 0;
  }
  long long a = 1;
  ans = 1e18;
  for (a = 2; a < As + 1; a++) {
    if (As % a == 0) {
      ans = min(ans, solve(a));
    }
  }
  printf("%lld", ans);
  return 0;
}
