#include <bits/stdc++.h>
using namespace std;
void fileio() {}
vector<long long> find_pf(long long n) {
  vector<long long> pf;
  long long tmp = n;
  for (long long i = 2; i * i <= n; i++) {
    if (tmp % i == 0) {
      pf.push_back(i);
      while (tmp % i == 0) tmp /= i;
    }
  }
  if (tmp > 1) pf.push_back(tmp);
  return pf;
}
long long find(vector<long long> a) {
  long long n = a.size();
  long long med = a[(n - 1) / 2];
  long long c = 0;
  for (long long i = 0; i < n; i++) c += abs(med - a[i]);
  return c;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  ;
  vector<long long> pos;
  for (long long i = 0; i < n; i++)
    if (a[i] == 1) pos.push_back(i);
  long long m = pos.size();
  if (m == 1) {
    cout << -1 << "\n";
    return;
  }
  vector<long long> pf = find_pf(m);
  long long ans = 9223372036854775807;
  for (auto k : pf) {
    long long c = 0;
    long long i = 0;
    while (i < m) {
      vector<long long> v;
      while (v.size() < k) v.push_back(pos[i]), i++;
      c += find(v);
    }
    ans = min(ans, c);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  fileio();
  solve();
}
