#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, ans = 0, coun = 0;
  cin >> n;
  vector<long long> prev;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) coun++;
  }
  long long mii = 0;
  for (long long i = 2; i < n + 1; i++) {
    if (coun % i == 0) {
      mii = i;
      break;
    }
  }
  if (mii == 0) {
    cout << "-1\n";
    return 0;
  }
  long long answer = 1000000000000;
  for (long long mi = 2; mi < n + 1; mi++) {
    if (coun % mi == 0) {
      ans = 0;
      for (long long i = 0; i < n; i++) {
        if (a[i] == 1) {
          prev.push_back(i);
          if (prev.size() % mi == 0) {
            for (long long k = 0; k < mi / 2; k++)
              ans += prev[prev.size() - 1 - k] - prev[prev.size() - (mi) + k];
          }
        }
      }
      answer = min(ans, answer);
    }
  }
  cout << answer << endl;
  return 0;
}
