#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long solve(vector<long long> vec, long long x, long long y) {
  long long ans = 0;
  for (long long i = 0; i < x; i += y) {
    long long idx = (i * 2 + y - 1) / 2, k = vec[idx];
    for (long long j = i; j < i + y; j++) {
      ans += abs(vec[j] - vec[idx]);
    }
  }
  return ans;
}
signed main() {
  long long n;
  cin >> n;
  vector<long long> v(n + 1), vec;
  for (long long i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] == 1) vec.push_back(i);
  }
  long long x = vec.size(), y = -1, ans = INT_MAX;
  ans *= 10000LL;
  for (long long i = 2; i <= x; i++) {
    if (x % i == 0) {
      ans = min(ans, solve(vec, x, i));
      y = i;
    }
  }
  if (y == -1) {
    cout << y << '\n';
    return 0;
  }
  cout << ans;
}
