#include <bits/stdc++.h>
using namespace std;
long long int n, m, a[100010], x[100010], i, t, ans, ansf, k, y, j;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  m = 0;
  for (i = 1; i < n + 1; i++) {
    cin >> t;
    if (t == 1) {
      m++;
      a[m] = i;
    }
  }
  n = m;
  ans = 0;
  ansf = 1e15;
  y = trunc(sqrt(n));
  if (n == 1) {
    cout << -1;
    return 0;
  }
  for (j = 1; j <= y; j++)
    if (n % j == 0) {
      k = j;
      if (k != 1) {
        ans = 0;
        for (t = 1; t < n / k + 1; t++)
          for (i = 1; i < k + 1; i++)
            ans = ans + abs(a[k * (t - 1) + i] - a[k * (t - 1) + k / 2 + 1]);
        ansf = min(ansf, ans);
      }
      k = n / j;
      ans = 0;
      for (t = 1; t < n / k + 1; t++)
        for (i = 1; i < k + 1; i++)
          ans = ans + abs(a[k * (t - 1) + i] - a[k * (t - 1) + k / 2 + 1]);
      ansf = min(ansf, ans);
    }
  cout << ansf;
}
