#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unrolled-loops")
using namespace std;
const long long mod = 1e9 + 7;
const long long mod2 = 1e9 + 9;
const long long INF = 1e15;
const long long oo = 1e18 + 7;
int test = 1;
void db();
void Varvalian() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ifstream *fi = new ifstream("TestCode.inp");
  ofstream *fo = new ofstream("TestCode.out");
  if (*fi) {
    cin.rdbuf(fi->rdbuf());
    cout.rdbuf(fo->rdbuf());
  }
}
long long n, s;
long long a[100100];
vector<long long> d;
bool isPrime(long long a) {
  if (a < 2) return 0;
  for (long long i = 2; i <= sqrt(a); i++)
    if (a % i == 0) return 0;
  return 1;
}
void In() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    s += a[i];
  }
}
void Solve() {
  In();
  if (s == 1) {
    cout << -1;
    return;
  }
  long long x;
  for (long long i = 2; i <= s; i++)
    if (s % i == 0) d.push_back(i);
  vector<long long> v;
  long long cnt = 0;
  long long c;
  for (long long it = 0; it < d.size(); it++) {
    x = d[it];
    c = 0;
    for (long long i = 1; i <= n; i++) {
      if (a[i] == 1) v.push_back(i);
      if (v.size() == x) {
        long long mid = x / 2;
        for (long long j = 0; j < v.size(); j++) c += abs(v[mid] - v[j]);
        v.clear();
      }
    }
    if (cnt == 0)
      cnt = c;
    else
      cnt = min(cnt, c);
  }
  cout << cnt;
}
void db() {}
int32_t main() {
  Varvalian();
  Solve();
}
