#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
long long fast_exp(long long base, long long exp) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) res = (res * base) % 1000000007;
    base = (base * base) % 1000000007;
    exp /= 2;
  }
  return res % 1000000007;
}
int gcd(int a, int b) {
  while (a && b) a > b ? a %= b : b %= a;
  return a + b;
}
int val(char c) {
  if (c >= '0' && c <= '9')
    return (int)c - '0';
  else
    return (int)c - 'A' + 10;
}
long long pows(int a, int b) {
  long long res = 1;
  for (int i = 0; i < b; ++i) {
    res *= a;
  }
  return res;
}
long long logx(long long base, long long num) {
  int cnt = 0;
  while (num != 1) {
    num /= base;
    ++cnt;
  }
  return cnt;
}
long long divisibles(long long a, long long b, long long m) {
  if (a % m == 0)
    return (b / m) - (a / m) + 1;
  else
    return (b / m) - (a / m);
}
string bitstring(int n) {
  string s;
  while (n) {
    s += (n % 2) + '0';
    n /= 2;
  }
  reverse(s.begin(), s.end());
  return s;
}
vector<int> root(200001, 0);
vector<int> size(200001, 1);
int find(int x) {
  while (x != root[x]) x = root[x];
  return x;
}
bool same(int a, int b) { return find(a) == find(b); }
void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (size[a] < size[b]) swap(a, b);
  size[a] += size[b];
  root[b] = a;
}
vector<int> vis(200001, 0);
vector<int> adj[200001];
int main() {
  std::ios::sync_with_stdio(false);
  vector<long long> primes;
  for (int i = 2; i <= 100000; ++i) {
    int flag = 0;
    for (int j = 2; j <= sqrtl(i); ++j) {
      if (i % j == 0) ++flag;
    }
    if (flag == 0) primes.push_back(i);
  }
  int n;
  cin >> n;
  vector<long long> vec(n);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    if (vec[i] == 1) ++sum;
  }
  if (sum == 1) {
    cout << -1 << "\n";
    return 0;
  }
  long long mini = pows(10, 18);
  for (int i = 0; i < primes.size(); ++i) {
    long long prime = primes[i];
    if (sum % prime) continue;
    vector<long long> tempx;
    long long sumprime = 0;
    long long median = 0;
    for (int j = 0; j < n; ++j) {
      if (vec[j] == 1) {
        tempx.push_back(j);
        if (tempx.size() == prime) {
          median = tempx[prime / 2];
          for (int k = 0; k < tempx.size(); ++k) {
            sumprime += abs(median - tempx[k]);
          }
          tempx.clear();
          median = 0;
        }
      }
    }
    mini = min(sumprime, mini);
  }
  cout << mini << "\n";
}
