#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
int64_t numNums;
int64_t arr[2000000], sum = 0;
int64_t locs[2000000], numLocs;
int64_t calcAnswer(int64_t factor) {
  int64_t i, j;
  int64_t answer = 0;
  for (i = 0; i < sum; i += factor) {
    int64_t *curLocs = locs + i;
    for (j = 0; j < factor - j - 1; j++) {
      answer += curLocs[factor - j - 1] - curLocs[j];
    }
  }
  return answer;
}
int main() {
  scanf("%" PRId64, &numNums);
  int64_t i, j;
  for (i = 0; i < numNums; i++) {
    scanf("%" PRId64, arr + i);
    sum += arr[i];
    if (arr[i] > 0) locs[numLocs++] = i;
  }
  if (sum == 1)
    printf("-1\n");
  else {
    int64_t factor;
    int64_t answer = -1;
    for (factor = 2; factor <= sum; factor++) {
      if ((sum % factor) == 0) {
        int64_t possAnswer = calcAnswer(factor);
        if ((answer == -1) || (possAnswer < answer)) answer = possAnswer;
      }
    }
    printf("%" PRId64 "\n", answer);
  }
  return 0;
}
