#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100100;
const long long INF = 2000000000000100;
const long long MOD = 998244353;
int N, C, A[MAXN];
vector<int> P;
long long ans = INF;
inline long long mult(long long a, long long b) { return (a * b) % MOD; }
inline long long add(long long a, long long b) { return (a + b) % MOD; }
int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i]) {
      P.push_back(i);
      C++;
    }
  }
  for (int f = 2; f <= C; f++) {
    if (C % f == 0) {
      int b = C / f;
      long long x = 0;
      for (int i = 0; i < b; i++) {
        for (int j = 0; j < f; j++) {
          x += abs(P[i * f + j] - P[i * f + f / 2]);
        }
      }
      ans = min(ans, x);
    }
  }
  if (ans == INF)
    cout << "-1\n";
  else
    cout << ans << "\n";
}
