#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vll = vector<ll>;
using si = set<int>;
using sll = set<ll>;
ll check(vi &a, int md) {
  vi tmp;
  int n = a.size();
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i]) {
      tmp.push_back(i);
      if (tmp.size() == md) {
        for (int j = 0; j < md; j++) {
          ans += abs(tmp[j] - tmp[j / 2]);
        }
        tmp.clear();
      }
    }
  }
  return ans;
}
const int nax = 1e6;
bitset<nax> bs;
vi primes;
void sieve() {
  bs.set();
  bs[0] = bs[1] = 0;
  for (int i = 2; i < nax; i++) {
    if (bs[i]) {
      primes.push_back(i);
      for (ll j = (ll)i * i; j < nax; j += i) {
        bs[j] = 0;
      }
    }
  }
}
vi factorize(int x) {
  vi ans;
  int idx = 0;
  int n = primes.size();
  while (x > 1 && idx < n) {
    if (x % primes[idx] == 0) {
      ans.push_back(primes[idx]);
      while (x % primes[idx] == 0) x /= primes[idx];
    }
    idx++;
  }
  if (x > 1) ans.push_back(x);
  return ans;
}
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  sieve();
  int n;
  cin >> n;
  vi a(n);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i]) cnt++;
  }
  if (cnt < 2) {
    cout << -1 << endl;
  } else {
    vi divisors = factorize(cnt);
    divisors.push_back(cnt);
    ll ans = 1e17;
    for (int i = 0; i < divisors.size(); i++) {
      ans = min(ans, check(a, divisors[i]));
    }
    cout << ans << endl;
  }
  return 0;
}
