#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
const int N = 1e5 + 7;
const int mod = 998244353;
const long long inf = 1e18 + 7;
long long n, t, ans, sm;
vector<int> pos;
int main() {
  cin >> n;
  sm = 0;
  for (int i = 1; i <= n; i++) {
    cin >> t, sm += t;
    if (t) pos.push_back(i);
  }
  if (sm == 1) {
    cout << "-1\n";
    return 0;
  } else if (sm == 2) {
    cout << pos[1] - pos[0] << "\n";
    return 0;
  } else if (sm == 3) {
    cout << pos[1] - pos[0] + pos[2] - pos[1] << "\n";
    return 0;
  }
  ans = inf;
  long long now = 0, st = 0;
  for (; st < sm; st += sm) {
    long long mid = (st + st + sm) >> 1;
    for (int j = st, cnt = 1; cnt <= sm; cnt++, j++) {
      now += abs(pos[j] - pos[mid]);
    }
  }
  ans = min(ans, now);
  for (int i = 2; i <= sqrt(sm); i++) {
    if (sm % i) continue;
    long long now = 0, st = 0;
    for (; st < sm; st += i) {
      long long mid = (st + st + i) >> 1;
      for (int j = st, cnt = 1; cnt <= i; cnt++, j++) {
        now += abs(pos[j] - pos[mid]);
      }
    }
    ans = min(ans, now);
    long long len = sm / i;
    now = 0, st = 0;
    for (; st < sm; st += len) {
      long long mid = (st + st + len) >> 1;
      for (int j = st, cnt = 1; cnt <= len; cnt++, j++) {
        now += abs(pos[j] - pos[mid]);
      }
    }
    ans = min(ans, now);
  }
  cout << ans << "\n";
  return 0;
}
