#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
int n;
int A[MAXN + 10];
long long f(vector<int>& s) {
  int m = (int)s.size();
  int p = (m - 1) / 2;
  long long tot = 0;
  for (int i = 0; i < m; i++) tot += abs(s[p] - s[i]);
  return tot;
}
int main() {
  scanf("%d", &n);
  int tot = 0;
  for (int i = 0; i < n; i++) scanf("%d", &A[i]), tot += A[i];
  if (tot == 1)
    printf("-1\n");
  else {
    int d = 2;
    while (tot % d) d++;
    vector<int> s;
    long long ans = LLONG_MAX;
    int p = 2;
    while (tot > 1) {
      if (tot % p == 0) {
        long long cur = 0;
        for (int i = 0; i < n; i++) {
          if (A[i]) {
            s.push_back(i);
            if (s.size() == p) {
              cur += f(s);
              s.clear();
            }
          }
        }
        ans = min(ans, cur);
        while (tot % p == 0) tot /= p;
      }
      p++;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
