#include <bits/stdc++.h>
using namespace std;
using namespace std;
void io() {}
void solve() {
  io();
  long long int n;
  cin >> n;
  long long int a[n];
  long long int num = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      num++;
    }
  }
  if (num == 1) {
    std::cout << -1 << '\n';
  } else {
    std::vector<long long int> v;
    long long int k = 0;
    for (long long int i = 2; i < n; i++) {
      if (num % i == 0) {
        v.push_back(i);
        while (num % i == 0) {
          num = num / i;
        }
      }
    }
    long long int ans = 0;
    long long int trueans = LLONG_MAX;
    long long int count = 0;
    for (long long int j = 0; j < v.size(); j++) {
      ans = 0;
      for (long long int i = 0; i < n; i++) {
        if (count < (v[j] + 1) / 2) {
          ans = ans + count;
        } else {
          ans = ans + (v[j] - count);
        }
        if (a[i] == 1) {
          count++;
        }
        if (count == v[j]) {
          count = 0;
        }
      }
      trueans = min(trueans, ans);
    }
    std::cout << trueans << '\n';
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  solve();
  return 0;
}
