#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
template <class I>
inline void read(I &x) {
  int f = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       x = (x << 3) + (x << 1) + (c & 15), c = getchar())
    ;
  x *= f;
}
int a[N], n;
long long ans = 1ll << 61, sum;
long long calc(long long p) {
  long long res = 0, now = 0;
  for (int i = 1; i <= n; i++) {
    now = (now + a[i]) % p;
    res += min(now, p - now);
  }
  return res;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]), sum += a[i];
  if (sum == 1) {
    puts("-1");
    return 0;
  }
  for (int i = 2; 1ll * i * i <= sum; i++) {
    if (sum % i == 0) ans = min(ans, calc(i));
    while (sum % i == 0) sum /= i;
  }
  if (sum > 1) ans = min(ans, calc(sum));
  printf("%lld\n", ans);
  return 0;
}
