#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long n, a[100007], x, ans, t, res, tmp;
vector<long long> v, cur;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long int i = 0; i < n; i++) {
    cin >> x;
    if (x) v.push_back(i);
  }
  if (v.size() < 2) {
    cout << -1;
    return 0;
  }
  if (v.size() < 4) {
    if (v.size() == 2)
      cout << v[1] - v[0];
    else
      cout << v[2] - v[0];
    return 0;
  }
  ans = 1e12;
  for (long int i = 2; i * i <= v.size(); i++) {
    if (v.size() % i != 0) continue;
    tmp = i;
    for (long int k = 0; k < 2; k++, i = v.size() / i) {
      t = 0;
      res = 0;
      for (auto j : v) {
        cur.push_back(j);
        t++;
        if (t % i == 0) {
          t = 0;
          for (auto h : cur) res += abs(cur[i / 2] - h);
          cur.clear();
        }
      }
      ans = min(ans, res);
    }
    i = tmp;
  }
  res = 0;
  for (auto i : v) {
    res += abs(i - v[v.size() / 2]);
  }
  cout << min(res, ans);
}
