#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long n, i;
  cin >> n;
  vector<long long> pos;
  pos.reserve(n);
  for (i = (0); i < (n); ++i) {
    long long a;
    cin >> a;
    if (a == 1) pos.push_back(i);
  }
  if (pos.size() == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long total = pos.size();
  auto comp = [&pos](long long m) {
    long long ans = 0, i;
    for (i = 0; i < int((pos).size()); i += m) {
      long long j = 0;
      while (i + m - 1 - j > i + j) {
        ans += (pos[i + m - 1 - j] - pos[i + j]);
        ++j;
      }
    }
    return ans;
  };
  long long p = 2;
  long long ans = comp(total);
  while (p * p <= total) {
    if (total % p == 0) {
      ans = min(ans, comp(p));
      ans = min(ans, comp(total / p));
    }
    ++p;
  }
  cout << ans << endl;
  return 0;
}
