#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
int main() {
  long long int n;
  cin >> n;
  long long int a[n];
  long long int k = 0;
  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i]) {
      v.push_back(i);
      k++;
    }
  }
  vector<long long int> div;
  for (long long int i = 2; i * i <= k; i++) {
    if (k % i == 0) {
      div.push_back(i);
      if (k / i != i) div.push_back(k / i);
    }
  }
  if (k == 1) {
    cout << -1 << "\n";
    return 0;
  }
  div.push_back(k);
  long long int ans = 1e18;
  for (long long int j : div) {
    long long int tot = 0;
    vector<long long int> temp;
    for (long long int i : v) {
      temp.push_back(i);
      if ((long long int)temp.size() == j) {
        long long int x = j / 2;
        long long int x1 = x + 1;
        long long int sum1 = 0;
        long long int sum2 = 0;
        for (long long int l : temp) {
          if (x1 < j) sum1 += abs(temp[x1] - l);
          sum2 += abs(temp[x] - l);
        }
        if (x1 < j) sum2 = min(sum2, sum1);
        tot += sum2;
        temp.clear();
      }
    }
    ans = min(ans, tot);
  }
  cout << ans << "\n";
}
