#include <bits/stdc++.h>
const int MAXN = 1e6 + 5;
long long a[MAXN];
int n;
inline long long get(long long d) {
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) ans += std::min(a[i] % d, d - a[i] % d);
  return ans;
}
int main() {
  scanf("%d", &n);
  long long sm = 0, mn = 1e18;
  for (long long i = 1; i <= n; ++i) scanf("%lld", a + i), sm += a[i];
  for (long long i = 1; i <= n; ++i)
    if (a[i]) mn = std::min(mn, a[i]);
  if (sm == 1) {
    puts("-1");
    return 0;
  }
  for (long long i = 2; i <= n; ++i) a[i] += a[i - 1];
  long long q = std::sqrt(1.0 * sm), ans = 1e18;
  for (long long i = 2; i <= q; ++i) {
    if (!(sm % i)) {
      while (!(sm % i)) sm /= i;
      ans = std::min(ans, get(i));
    }
  }
  if (sm > 1) ans = std::min(ans, get(sm));
  printf("%lld\n", ans);
  return 0;
}
