#include <bits/stdc++.h>
using namespace std;
vector<int> a, pos;
long long combine(int k) {
  long long ret = 0ll;
  for (int i = 0; i < (int)pos.size(); i += k) {
    int p = pos[(i + i + k - 1) / 2];
    for (int j = i, c = 0; c < k && j < (int)pos.size(); ++j, ++c) {
      ret += abs(p - pos[j]);
    }
  }
  return ret;
}
void compute() {
  a.clear();
  int n;
  cin >> n;
  a.resize(n);
  int one = 0;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i];
    one += (a[i] == 1);
    if (a[i] == 1) {
      pos.push_back(i);
    }
  }
  if (n == 1 || one == 1) {
    cout << -1 << "\n";
    return;
  }
  long long res = combine(one);
  for (int i = 2; i * i <= one; ++i) {
    if (one % i == 0) {
      res = min(res, combine(one / i));
      int p = one / i;
      if (p != i) res = min(res, combine(i));
    }
  }
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) compute();
  return 0;
}
