#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int main() {
  long long n;
  scanf("%lld", &n);
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  if (sum == 1) {
    printf("-1\n");
    return 0;
  }
  if (sum == 0) {
    printf("0\n");
  }
  long long tsum = sum;
  long long ans = 1000000000000000ll;
  for (long long p = 2; p <= tsum; p++) {
    if (tsum % p == 0) {
      while (tsum % p == 0) tsum /= p;
      long long l = 1, r = 1, ans1 = 0;
      for (long long i = 1; i <= n; i++) {
        if (a[i]) {
          l = i;
          r = i - 1;
          long long t = p;
          while (t) {
            r++;
            if (a[r]) t--;
          }
          long long coun = 0, pos = 0;
          for (long long j = l; j <= r; j++) {
            if (a[j]) coun++;
            if (coun == (p + 1) / 2) {
              pos = j;
              break;
            }
          }
          for (long long j = l; j <= r; j++) {
            if (a[j]) ans1 += abs(j - pos);
          }
          i = r;
        }
      }
      ans = min(ans, ans1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
