#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 100010;
const long long INF = (1LL << 60);
int a[MAXN], b[MAXN];
int N, sum;
long long ans = INF;
inline long long calc(int t) {
  int mid = (t + 1) >> 1, Len = 0;
  long long Ret = 0;
  for (int i = 1; i <= N; ++i)
    if (a[i]) {
      b[++Len] = i;
      if (Len == t) {
        for (int j = 1; j <= Len; ++j) Ret += abs(b[j] - b[mid]);
        Len = 0;
      }
    }
  return Ret;
}
int main() {
  N = read();
  for (int i = 1; i <= N; ++i) {
    a[i] = read();
    sum += a[i];
  }
  if (sum < 2) {
    puts("-1");
    return 0;
  }
  for (int i = 2; i <= sum; ++i)
    if (sum % i == 0) ans = min(ans, calc(i));
  printf("%I64d\n", ans);
  return 0;
}
