#include <bits/stdc++.h>
using namespace std;
long long pre[100005], a[100005], n;
long long f(int l, int r, long long num) {
  long long cnt = 0, fres = 1000000000000000, res = 0;
  for (int i = l; i <= r; i++) {
    if (a[i] == 1) cnt++;
    res = cnt * i - (pre[i] - pre[l - 1]);
    res = (pre[r] - pre[i]) - (num - cnt) * i + res;
    fres = min(res, fres);
  }
  return fres;
}
long long solve(int num) {
  long long ans = 0;
  for (int i = 1; i <= n;) {
    if (a[i] == 1) {
      int st = i, cnt = 0;
      while (cnt < num) {
        if (a[i] == 1) cnt++;
        i++;
      }
      ans += f(st, i - 1, num);
    } else
      i++;
  }
  return ans;
}
int main() {
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre[i] = pre[i - 1];
    if (a[i] == 1) pre[i] += i;
    ;
    sum += a[i];
  }
  long long num = 1;
  for (int i = 2; i <= sum; i++)
    if (sum % i == 0) {
      num = i;
      break;
    }
  if (num == 1) {
    cout << -1;
    exit(0);
  }
  long long ans = 1000000000000000;
  for (int i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      ans = min(solve(i), ans);
      ans = min(solve(sum / i), ans);
    }
  }
  ans = min(ans, solve(sum));
  cout << ans;
}
