#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
  int N;
  cin >> N;
  int suma = 0;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    suma += a[i];
  }
  if (suma == 1) {
    cout << -1 << endl;
  } else {
    long long answer = LONG_LONG_MAX;
    for (int tk = 2; tk <= suma; ++tk) {
      if (suma % tk == 0) {
        int currenti = 0;
        long long cans = 0;
        while (currenti < N) {
          int currentsum = 0;
          int median = -1;
          int starti = currenti;
          while (currentsum < tk) {
            currentsum += a[currenti];
            if (a[currenti] == 1 && currentsum == (tk + 1) / 2) {
              median = currenti;
            }
            currenti++;
          }
          long long steps = 0;
          for (int i = starti; i < currenti; ++i) {
            if (a[i] == 1) {
              steps += abs(median - i);
            }
          }
          cans += steps;
        }
        answer = min(answer, cans);
      }
    }
    cout << answer << endl;
  }
  return 0;
}
