#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  deque<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  while (a.front() == 0) a.pop_front();
  while (a.back() == 0) a.pop_back();
  if (a.size() == 1) {
    cout << "-1\n";
    return 0;
  }
  long long ones = 0;
  for (long long x : a)
    if (x == 1) ones++;
  long long res = LLONG_MAX;
  for (long long d = 2; d <= ones; d++) {
    if (ones % d) continue;
    long long curr = 0;
    vector<long long> indices;
    for (long long i = 0; i < a.size(); i++) {
      if (a[i] == 1) indices.push_back(i);
      if (indices.size() == d) {
        long long median = indices[d / 2];
        for (long long index : indices) curr += abs(median - index);
        indices.clear();
      }
    }
    res = min(res, curr);
  }
  cout << res << "\n";
}
