#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long arr[maxn];
int main() {
  long long n;
  scanf("%lld", &n);
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", arr + i);
    sum += arr[i];
  }
  vector<long long> divs;
  for (long long i = 2; i <= sum; i++) {
    if (sum % i == 0) {
      divs.push_back(i);
    }
  }
  if (divs.size() == 0) {
    printf("-1\n");
    return 0;
  }
  long long total_ans = 1000000000000000;
  for (long long i = 0; i < divs.size(); i++) {
    long long least_div = divs[i];
    long long startt = -1;
    long long curr_sum = 0;
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      if (startt == -1) {
        startt = i;
        curr_sum = 0;
      }
      curr_sum += arr[i];
      if (curr_sum == least_div) {
        long long tempsum2 = 0;
        long long idx = -1;
        long long tempans = 0;
        for (long long j = startt; j <= i; j++) {
          tempsum2 += arr[j];
          if (tempsum2 == (least_div + 1) / 2) {
            idx = j;
            break;
          }
        }
        tempsum2 = 0;
        for (long long j = startt; j <= i; j++) {
          tempsum2 += arr[j];
          if (arr[j] == 1) {
            if (tempsum2 < (least_div + 1) / 2) {
              tempans += idx - j;
            } else if (tempsum2 > (least_div + 1) / 2) {
              tempans += j - idx;
            }
          }
        }
        ans += tempans;
        startt = -1;
      }
    }
    total_ans = min(total_ans, ans);
  }
  printf("%lld\n", total_ans);
  return 0;
}
