#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INF = 1e18;
int n;
int a[N];
long long get_cost(int l, int r) {
  long long dist = 0;
  long long cnt_left = 1, cnt_right = 0;
  for (int i = l + 1; i <= r; i++)
    if (a[i]) {
      cnt_right++;
      dist += i - l;
    }
  long long res = dist;
  for (int i = l + 1; i <= r; i++) {
    dist += cnt_left - cnt_right;
    res = min(res, dist);
    if (a[i]) {
      cnt_left++;
      cnt_right--;
    }
  }
  return res;
}
long long ans = INF;
void solve(int x) {
  int last = 0;
  long long res = 0, cnt = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] == 1) {
      last = i - 1;
      break;
    }
  for (int i = 1; i <= n; i++) {
    cnt += a[i];
    if (cnt % x == 0) {
      res += get_cost(last + 1, i);
      last = i;
      cnt = 0;
    }
  }
  ans = min(ans, res);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long sm = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sm += a[i];
  }
  if (sm == 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 2; i <= sm; i++) {
    if (sm % i == 0) {
      solve(i);
      while (sm % i == 0) sm /= i;
    }
  }
  cout << ans << endl;
  return 0;
}
