#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int cnt = 0;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    v[i] = temp;
    cnt += temp;
  }
  if (cnt == 1) {
    cout << "-1\n";
    return 0;
  }
  int sq = sqrt(cnt);
  long long ans = (long long)1000000000 * 1000000000;
  int fact = cnt;
  for (int i = 1; i <= sq; i++) {
    if (cnt % i) continue;
    fact = i;
    if (fact != 1) {
      long long tans = 0;
      int prev = 0;
      for (int i = 0; i < cnt / fact; i++) {
        vector<int> x;
        for (; prev < n && x.size() < fact; prev++) {
          if (v[prev]) x.push_back(prev);
        }
        int ind = x[fact / 2];
        for (auto it : x) tans += abs(it - ind);
      }
      ans = min(ans, tans);
    }
    long long tans = 0;
    fact = cnt / fact;
    int prev = 0;
    for (int i = 0; i < cnt / fact; i++) {
      vector<int> x;
      for (; prev < n && x.size() < fact; prev++) {
        if (v[prev]) x.push_back(prev);
      }
      int ind = x[fact / 2];
      for (auto it : x) tans += abs(it - ind);
    }
    ans = min(ans, tans);
  }
  cout << ans << endl;
  return 0;
}
