#include <bits/stdc++.h>
using namespace std;
int n, a[100005], cnt, idx, arr[100005];
long long ans, cost, cost1, cost2;
int main() {
  ans = 2e18;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    cnt += a[i];
  }
  if (cnt == 1) {
    printf("-1\n");
    return 0;
  }
  for (int x = 2; x <= cnt; ++x) {
    if (cnt % x != 0) continue;
    cost = 0;
    idx = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] == 0) continue;
      arr[idx] = i;
      ++idx;
      if (idx == x) {
        idx = 0;
        if (x % 2 == 1) {
          for (int j = 0; j < x; ++j) cost += abs(arr[j] - arr[x / 2]);
        } else {
          cost1 = cost2 = 0;
          for (int j = 0; j < x; ++j) cost1 += abs(arr[j] - arr[x / 2 - 1]);
          for (int j = 0; j < x; ++j) cost2 += abs(arr[j] - arr[x / 2]);
          cost += (cost1 < cost2 ? cost1 : cost2);
        }
      }
    }
    ans = (ans < cost ? ans : cost);
  }
  printf("%lld\n", ans);
  return 0;
}
