#include <bits/stdc++.h>
using namespace std;
int n, sum, ed, tot, st, mid, j;
int a[200005], b[200005];
long long ans, tem;
int main() {
  scanf("%d", &n);
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
    if (a[i]) b[++tot] = i;
  }
  j = 0;
  int fl = 0;
  ans = 10000000000000ll;
  for (int i = 1; i * i <= sum; i++)
    if (sum % i == 0) {
      j = i;
      tem = 0;
      if (j != 1) {
        fl = 1;
        for (int i = 1; i <= tot; i += j) {
          st = i;
          ed = i + j - 1;
          mid = (st + ed) / 2;
          for (int o = st; o <= ed; o++) tem += abs(b[mid] - b[o]);
        }
        ans = min(ans, tem);
      }
      j = sum / i;
      if (j != 1) {
        fl = 1;
        tem = 0;
        for (int i = 1; i <= tot; i += j) {
          st = i;
          ed = i + j - 1;
          mid = (st + ed) / 2;
          for (int o = st; o <= ed; o++) tem += abs(b[mid] - b[o]);
        }
        ans = min(ans, tem);
      }
    }
  if (fl)
    printf("%I64d\n", ans);
  else
    puts("-1");
  return 0;
}
