#include <bits/stdc++.h>
using namespace std;
int inp[100005];
long long lsum[100005], rsum[100005];
long long getcost(int a, int b, int total_cnt, int j, int n_one, int cnt) {
  long long cost(0);
  cost += (long long)cnt * j;
  cost -= lsum[total_cnt - 1];
  if (a - 1 >= 0) {
    cost += lsum[a - 1];
  }
  if (total_cnt < n_one) {
    cost += rsum[total_cnt];
  }
  if (b + 1 < n_one) {
    cost -= rsum[b + 1];
  }
  cost -= (long long)(b - a + 1 - cnt) * j;
  return cost;
}
int main() {
  int n;
  cin >> n;
  vector<int> indices;
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
    if (inp[i] == 1) {
      indices.push_back(i);
    }
  }
  int n_one(indices.size());
  if (n_one == 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n_one; ++i) {
    if (i == 0) {
      lsum[i] = indices[i];
    } else {
      lsum[i] = lsum[i - 1] + indices[i];
    }
  }
  for (int i = n_one - 1; i >= 0; --i) {
    if (i == n_one - 1) {
      rsum[i] = indices[i];
    } else {
      rsum[i] = rsum[i + 1] + indices[i];
    }
  }
  long long ans((long long)n * (long long)n);
  for (int i = 2; i <= n_one; ++i) {
    if (n_one % i != 0) {
      continue;
    }
    long long sum(0);
    int total_cnt(0);
    int cnt(0);
    long long local_min((long long)n * (long long)n);
    for (int j = 0; j < n; ++j) {
      if (inp[j] == 1) {
        ++total_cnt;
        ++cnt;
      }
      if (cnt == 0) {
        continue;
      }
      int a = total_cnt - cnt;
      int b = a + i - 1;
      local_min = min(local_min, getcost(a, b, total_cnt, j, n_one, cnt));
      if (cnt == i) {
        sum += local_min;
        cnt = 0;
        local_min = (long long)n * (long long)n;
      }
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
