#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> a(n);
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << -1 << endl;
    return;
  }
  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    if (a[i]) v.push_back(i);
  }
  long long int ans1 = LLONG_MAX;
  for (long long int i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      long long int c = 0, l = 0, r = 0, poi = 0, ans = 0;
      for (long long int j = 0; j < v.size(); j += i) {
        long long int m = v[(j + j + i - 1) / 2];
        for (long long int k = j; k < j + i; k++) {
          ans += abs(m - v[k]);
        }
      }
      ans1 = min(ans1, ans);
      c = 0;
      l = 0;
      r = 0;
      poi = 0;
      ans = 0;
      for (long long int j = 0; j < v.size() && sum / i != i; j += sum / i) {
        long long int m = v[(j + j + sum / i - 1) / 2];
        for (long long int k = j; k < j + sum / i; k++) {
          ans += abs(m - v[k]);
        }
      }
      ans1 = min(ans1, ans);
    }
  }
  long long int i = sum;
  long long int c = 0, l = 0, r = 0, poi = 0, ans = 0;
  for (long long int j = 0; j < v.size(); j += i) {
    long long int m = v[(j + j + i - 1) / 2];
    for (long long int k = j; k < j + i; k++) {
      ans += abs(m - v[k]);
    }
  }
  ans1 = min(ans1, ans);
  cout << ans1 << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
