#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x7fffffffffffffff;
const int maxn = 100000 + 5;
int n;
vector<int> vec;
long long sum[maxn];
long long res;
int lp;
int main() {
  scanf("%d", &n);
  vec.push_back(0);
  res = 1ll * inf;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == 1) vec.push_back(i);
  }
  for (int i = 1; i < vec.size(); i++) {
    sum[i] = sum[i - 1] + 1ll * vec[i];
  }
  lp = vec.size() - 1;
  for (int p = 2; p <= lp; p++) {
    if (lp % p == 0) {
      long long tmp = 0;
      for (int ls = 1; ls < vec.size(); ls++) {
        int rs = ls + p - 1;
        int mid = (ls + rs) >> 1;
        long long pre = sum[rs] - sum[mid] - 1ll * vec[mid] * (rs - mid);
        long long suc =
            1ll * vec[mid] * (mid - ls) - (sum[mid - 1] - sum[ls - 1]);
        tmp += (pre + suc);
        ls = rs;
      }
      res = min(res, tmp);
      if (lp / p > 1) {
        tmp = 0;
        for (int ls = 1; ls < vec.size(); ls++) {
          int rs = ls + lp / p - 1;
          int mid = (ls + rs) >> 1;
          long long pre = sum[rs] - sum[mid] - 1ll * vec[mid] * (rs - mid);
          long long suc =
              1ll * vec[mid] * (mid - ls) - (sum[mid - 1] - sum[ls - 1]);
          tmp += (pre + suc);
          ls = rs;
        }
        res = min(res, tmp);
      }
    }
    while (lp % p == 0) lp /= p;
  }
  if (res >= inf)
    printf("-1\n");
  else
    printf("%I64d\n", res);
  return 0;
}
