#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
int main() {
  cin >> n;
  int tot = 0;
  vector<int> idx;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    tot += a[i];
    if (a[i]) {
      idx.push_back(i);
    }
  }
  long long ans = 1LL << 62;
  for (int i = 2; i <= tot; ++i) {
    if (tot % i) continue;
    long long cur = 0;
    for (int j = 0; j < idx.size(); ++j) {
      int k = j / i;
      cur += (long long)abs(idx[k * i + i / 2] - idx[j]);
    }
    ans = min(ans, cur);
  }
  if (ans < 1LL << 62)
    cout << ans << endl;
  else
    cout << "-1" << endl;
  return 0;
}
