#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const long long MOD = 1000000007;
const int inf = 1 << 30;
const long long linf = 1LL << 60;
int n;
long long a[100000];
long long hoge[100000];
bool com[100001];
void init() {
  for (long long i = 2; i <= n; i++) {
    if (com[i]) continue;
    for (long long j = 2 * i; j <= n; j += i) {
      com[j] = true;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)n; i++) scanf("%lld", a + i);
  long long sum = 0;
  for (int i = 0; i < (int)n; i++) sum += a[i];
  if (sum <= 1) {
    printf("-1\n");
    return 0;
  }
  long long out = linf;
  for (long long i = 2; i <= sum; i++) {
    if (com[i]) continue;
    if (sum % i) continue;
    long long ans = 0;
    long long cnt = 0;
    long long mid = 0;
    for (int j = 0; j < (int)n; j++) {
      if (a[j]) {
        hoge[cnt++] = j;
        if (cnt == (i + 1) / 2) {
          mid = j;
        }
        if (cnt == i) {
          for (int k = 0; k < (int)cnt; k++) {
            ans += abs(mid - hoge[k]);
          }
          cnt = 0;
        }
      }
    }
    out = min(out, ans);
  }
  printf("%lld\n", out);
}
