#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 10;
int n, k, a[MAXN];
long long s[MAXN];
long long compute(int t) {
  long long ans = 0;
  for (int i = 1; i <= k; i += t) {
    int l = i;
    int r = i + t - 1;
    int m = (l + r) / 2;
    ans += (long long)a[m] * (m - l + 1) - (s[m] - s[l - 1]);
    ans += (s[r] - s[m]) - (long long)a[m] * (r - m);
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    if (x == 1) {
      a[++k] = i;
    }
  }
  for (int i = 1; i <= k; ++i) {
    s[i] = s[i - 1] + a[i];
  }
  long long ans = (long long)1e18;
  for (int i = 2; i <= k; ++i) {
    if (k % i == 0) {
      ans = min(ans, compute(i));
    }
  }
  if (ans == (long long)1e18) {
    ans = -1;
  }
  printf("%lld\n", ans);
  return 0;
}
