#include <bits/stdc++.h>
using namespace std;
inline void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  int k = 0;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    k += a[i];
  }
  if (k == 1) {
    cout << -1;
    return;
  }
  vector<int> prime;
  for (int i = 2; i * i <= k; i++) {
    while (k % i == 0) {
      k /= i;
      prime.push_back(i);
    }
  }
  if (k != 1) prime.push_back(k);
  long long otv_fin = INT64_MAX;
  for (int g = 0; g < (prime.size()); g++) {
    long long otv = 0;
    int pred = -1;
    int sum = 0;
    for (int i = 0; i < (n); i++) {
      if (a[i] == 1 && pred == -1) {
        pred = i;
        sum++;
      } else if (a[i] == 1 && pred != -1)
        sum++;
      if (sum == prime[g]) {
        int u = -1;
        int was = 0;
        for (int j = pred; j < (i + 1); j++) {
          if (a[j] == 1) {
            was++;
            u = j;
          }
          if (was == sum / 2 + sum % 2) break;
        }
        for (int j = pred; j < (i + 1); j++) {
          if (a[j] == 1) {
            otv += abs(j - u);
          }
        }
        pred = -1;
        sum = 0;
      }
    }
    otv_fin = min(otv, otv_fin);
  }
  cout << otv_fin;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
}
