#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int M = 1e9 + 7;
const double eps = 1e-9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> v(n + 1);
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    cnt += v[i];
  }
  if (cnt == 1) return cout << -1 << endl, 0;
  vector<long long> div;
  for (int i = 2; i <= cnt; ++i) {
    if (cnt % i == 0) {
      div.push_back(i);
    }
  }
  long long res = LLONG_MAX;
  for (int k = 0; k < div.size(); ++k) {
    long long lpf = div[k];
    vector<long long> pos;
    long long sum = 0, ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (v[i] == 1) sum += i, pos.push_back(i);
      if (pos.size() == lpf) {
        long long avg = pos[lpf / 2];
        sum = 0;
        for (int j = 0; j < lpf; ++j) {
          ans += abs(pos[j] - avg);
        }
        pos.clear();
      }
    }
    res = min(res, ans);
  }
  cout << res << endl;
}
