#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
using namespace std;
const int N = 1000003;
long long mod = 1e9 + 7;
long long a[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  a[0] = 0;
  long long i, j;
  for (i = 1; i <= n; ++i) cin >> a[i], a[i] += a[i - 1];
  long long x = a[n];
  if (x == 1) return cout << -1, 0;
  vector<long long> divs;
  for (i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      divs.push_back(i);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) divs.push_back(x);
  long long cnt, mi = (long long)(1e18 + 11);
  for (auto &it : divs) {
    cnt = 0;
    for (i = 1; i <= n; ++i) {
      x = a[i] % it;
      cnt += min(x, it - x);
    }
    mi = min(mi, cnt);
  }
  cout << mi;
  return 0;
}
