#include <bits/stdc++.h>
using namespace std;
int INF = 2e9;
vector<int> pref(string& s) {
  int n = s.size();
  vector<int> g(n, 0);
  for (int i = 1; i < n; i++) {
    int j = g[i - 1];
    while (j > 0 and s[i] != s[j]) {
      j = g[j - 1];
    }
    if (s[i] == s[j]) j++;
    g[i] = j;
  }
  return g;
}
int N = 3e5 + 10;
vector<long long> tree;
void build(vector<int>& g, int v, int tl, int tr) {
  if (tl == tr)
    tree[v] = g[tl];
  else {
    int tm = (tl + tr) / 2;
    build(g, 2 * v, tl, tm);
    build(g, 2 * v + 1, tm + 1, tr);
    tree[v] = max(tree[2 * v], tree[2 * v + 1]);
  }
}
int find_max(int v, int tl, int tr, int l, int r) {
  if (l > r) return 0;
  if (tl == l and tr == r) return tree[v];
  int tm = (tl + tr) / 2;
  return max(find_max(2 * v, tl, tm, l, min(tm, r)),
             find_max(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
void update(long long v, long long tl, long long tr, long long pos,
            long long val) {
  if (tl == tr)
    tree[v] = val;
  else {
    long long tm = (tl + tr) / 2;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, val);
    else
      update(2 * v + 1, tm + 1, tr, pos, val);
    tree[v] = max(tree[2 * v], tree[2 * v + 1]);
  }
}
long long bin_pow(long long a, long long b, long long m) {
  if (b == 0) return 0;
  if (b == 1) return a % m;
  if (b % 2 == 0) {
    long long c = bin_pow(a, b / 2, m);
    c %= m;
    return (c * c) % m;
  } else
    return ((a % m) * bin_pow(a, b - 1, m) % m) % m;
}
unsigned long long numbers(unsigned long long n) {
  unsigned long long a = 0;
  while (n != 0) {
    n /= 10;
    a++;
  }
  return a;
}
unsigned long long num(unsigned long long j, unsigned long long n1,
                       unsigned long long k) {
  unsigned long long t = 0, a = 0;
  while (a != n1 - k + 1) {
    t = j % 10;
    j /= 10;
    a++;
  }
  return t;
}
long long mod = 1e9 + 7;
char f(string s) {
  if (s[0] == 'a' and s[1] == 'b' or s[1] == 'a' and s[0] == 'b') return 'c';
  if (s[0] == 'a' and s[1] == 'c' or s[1] == 'a' and s[0] == 'c') return 'b';
  if (s[0] == 'b' and s[1] == 'c' or s[1] == 'b' and s[0] == 'c') return 'a';
}
struct wor {
  long long a, b, c, d;
};
bool cmp(wor w1, wor w2) { return w1.a > w2.a; }
bool cmp1(wor w1, wor w2) { return w1.c < w2.c; }
vector<int> ssize(3e5 + 10, 1);
vector<int> parent(3e5 + 10);
vector<int> ma(3e5 + 10);
int find_set(int v) {
  if (v == parent[v]) {
    return v;
  }
  return parent[v] = find_set(parent[v]);
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (ssize[a] < ssize[b]) {
      parent[a] = b;
      ssize[b] += ssize[a];
      ma[b] = max(ma[a], ma[b]);
    } else {
      parent[b] = a;
      ssize[a] += ssize[b];
      ma[a] = max(ma[a], ma[b]);
    }
  }
}
int f(int a) {
  int sum = 0;
  while (a != 0) {
    sum += a % 10;
    a /= 10;
  }
  return sum;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> g;
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a == 1) g.push_back(i);
  }
  if (g.size() == 1)
    cout << -1;
  else {
    long long ans1 = 1e15;
    long long p = g.size();
    if (g.size() % 2 == 0) {
      ans1 = 0;
      for (int i = 1; i < g.size(); i += 2) {
        ans1 += g[i] - g[i - 1];
      }
    }
    vector<long long> d;
    for (int i = 2; i <= g.size(); i++) {
      if (p == 1) break;
      if (p % i == 0) {
        d.push_back(i);
        p /= i;
        i--;
      }
    }
    long long ans = 1e15;
    ans = min(ans, ans1);
    for (int k = 0; k < d.size(); k++) {
      if (d[k] == 2) continue;
      long long sum = 0;
      for (int i = d[k] / 2; i < g.size(); i += d[k]) {
        for (int j = i - d[k] / 2; j < i; j++) {
          sum += g[i] - g[j];
        }
        for (int j = i + 1; j <= i + d[k] / 2; j++) {
          sum += g[j] - g[i];
        }
      }
      ans = min(ans, sum);
    }
    cout << ans;
  }
  return 0;
}
