#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int arr[n];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1) cnt++;
  }
  if (cnt == 1) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> factor;
  for (int i = 2; i <= cnt; i++) {
    if (cnt % i == 0) {
      factor.push_back(i);
    }
  }
  long long fans = 1e17;
  for (int j = 0; j < factor.size(); j++) {
    long long ans = 0;
    int num = factor[j];
    vector<int> v;
    int c = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] == 1) c++;
      if (c == num / 2 + 1 && arr[i] == 1) {
        v.push_back(i);
      }
      if (c == num) c = 0;
    }
    int p = 0;
    int k = 0;
    int l = v.size();
    while (p < l) {
      int cn = 0;
      while (cn < num) {
        if (arr[k] == 1) {
          cn++;
          ans += abs(v[p] - k);
        }
        k++;
      }
      p++;
    }
    fans = min(fans, ans);
  }
  cout << fans << endl;
  return 0;
}
