#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18 + 5;
const long long MOD = 1e9 + 7;
const long long MAXN = 1000005;
long long N, a[MAXN], sum;
vector<long long> all;
long long test(long long k) {
  long long res = 0;
  vector<long long> pos = all;
  while (!pos.empty()) {
    vector<long long> vec;
    for (long long i = 0; i < k; i++) {
      vec.push_back(pos.back());
      pos.pop_back();
    }
    long long mid = vec[(k - 1) / 2];
    for (long long x : vec) {
      res += abs(x - mid);
    }
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i]) {
      all.push_back(i);
      sum++;
    }
  }
  long long ans = INF;
  for (long long i = 2; i <= sum; i++) {
    if (sum % i == 0) {
      ans = min(ans, test(i));
    }
  }
  cout << (ans == INF ? -1 : ans) << '\n';
  return 0;
}
