#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
void warp() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
}
long long get(vector<long long> &a) {
  long long ans = 0;
  long long tar = a[a.size() / 2];
  for (long long i = 0; i < a.size(); i++) ans += abs(tar - a[i]);
  return ans;
}
long long check(long long need, vector<long long> a) {
  long long ans = 0;
  long long n = a.size();
  vector<long long> cur;
  for (long long i = 0; i < n; i++) {
    if (a[i]) cur.push_back(i);
    if (cur.size() == need) {
      ans += get(cur);
      cur.clear();
    }
  }
  return ans;
}
signed main() {
  warp();
  long long n;
  cin >> n;
  vector<long long> a(n);
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    cnt += a[i];
  }
  if (cnt == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 1e11;
  for (long long i = 2; i <= cnt; i++) {
    if (cnt % i == 0) ans = min(ans, check(i, a));
  }
  cout << ans << endl;
  return 0;
}
