#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll solve(vector<ll> pos, ll k) {
  ll ans = 0;
  for (ll i = 0; i < (ll)pos.size(); i += k) {
    ll idx1 = (2 * i + k - 1) / 2, idx2 = (2 * i + k) / 2, ans1 = 0, ans2 = 0;
    for (ll j = i; j < i + k; j++) {
      ans1 += abs(pos[j] - pos[idx1]);
      ans2 += abs(pos[j] - pos[idx2]);
    }
    ans += min(ans1, ans2);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, second = 0, ans = 1e18;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) cin >> arr[i], second += arr[i];
  if (second == 1) {
    cout << "-1\n";
    return 0;
  }
  vector<ll> pos;
  for (ll i = 0; i < n; i++)
    if (arr[i]) pos.push_back(i);
  for (ll i = 2; i * i <= second; i++)
    if (second % i == 0)
      ans = min(ans, min(solve(pos, i), solve(pos, second / i)));
  cout << min(solve(pos, second), ans) << "\n";
  return 0;
}
