#include <bits/stdc++.h>
using namespace std;
namespace FastIO {
template <typename tp>
inline void read(tp &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  for (; c < '0' || c > '9'; f |= (c == '-'), c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f) x = -x;
}
template <typename tp>
inline void write(tp x) {
  if (x == 0) return (void)(putchar('0'));
  if (x < 0) putchar('-'), x = -x;
  int pr[20];
  register int cnt = 0;
  for (; x; x /= 10) pr[++cnt] = x % 10;
  while (cnt) putchar(pr[cnt--] + '0');
}
template <typename tp>
inline void writeln(tp x) {
  write(x);
  putchar('\n');
}
}  // namespace FastIO
using namespace FastIO;
int n, m;
long long h[50];
int d[1000020];
bool check(long long x) {
  for (int i = 1; i <= m; ++i) {
    if (x % h[i] == 0) return 0;
  }
  return 1;
}
int main() {
  read(n);
  long long cnt = 0, ans = 0x3f3f3f3f3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    read(d[i]);
    cnt += d[i];
  }
  if (cnt == 1) {
    return puts("-1");
  }
  m = 0;
  for (int i = 2, maxn = sqrt(cnt); i <= maxn; ++i)
    if (cnt % i == 0) {
      if (check(i)) {
        h[++m] = i;
      }
      if (check(cnt / i)) {
        h[++m] = cnt / i;
      }
    }
  if (cnt > 1) h[++m] = cnt;
  for (int I = 1; I <= m; ++I) {
    long long pls = 0;
    long long x = h[I], delta = 0, Ans = 0;
    for (int i = 1; i <= n; ++i) {
      long long b = d[i] % x;
      if (!b) continue;
      if (delta) {
        if (delta + b < x) {
          if (delta < x - delta) {
            Ans += delta * (i - pls);
            pls = i;
          } else {
            Ans += b * (i - pls);
          }
          delta += b;
        } else {
          Ans += min(delta, x - delta) * (i - pls);
          delta = delta + b - x;
          pls = i;
        }
      } else {
        delta = b;
        pls = i;
      }
    }
    if (delta == 0) ans = min(ans, Ans);
  }
  writeln(ans);
  return 0;
}
