#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a) cin >> i;
  int s = accumulate(a.begin(), a.end(), 0);
  const long long inf = numeric_limits<long long>::max();
  long long ans = inf;
  for (int i = 2; i <= s; ++i)
    if (s % i == 0) {
      int cur = 0;
      long long cost = 0;
      vector<int> pos;
      for (int j = 0; j < n; ++j) {
        cur += a[j];
        if (a[j] > 0) pos.push_back(j);
        if (cur > 0 && cur % i == 0) {
          cur = 0;
          int p = pos.size() / 2;
          for (auto x : pos) cost += abs(x - pos[p]);
          pos.clear();
        }
      }
      if (cur == 0) ans = min(ans, cost);
    }
  if (ans == inf) ans = -1;
  cout << ans << "\n";
  return 0;
}
