#include <bits/stdc++.h>
using namespace std;
mt19937_64 gen;
void run() {
  int n;
  cin >> n;
  vector<int> a(n), pos;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i]) pos.emplace_back(i);
  }
  if ((long long)pos.size() == 1) {
    cout << -1;
    return;
  }
  long long ans = 1e18;
  for (int k = 2; k <= (long long)pos.size(); k++) {
    if ((long long)pos.size() % k == 0) {
      long long res = 0;
      for (int j = 0; j * k < (long long)pos.size(); j++) {
        for (int i = 0; i < k; i++) {
          res += abs(pos[i + j * k] - pos[j * k + k / 2]);
        }
      }
      ans = min(ans, res);
    }
  }
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
  run();
}
