#include <bits/stdc++.h>
using namespace std;
const long long MAXN = (1LL << 20) + 10;
void fi(const char* tem) {
  if (!strlen(tem)) return;
  char A[50];
  const char B[4] = {'.', 'i', 'n', 0}, C[5] = {'.', 'o', 'u', 't', 0};
  memset(A, 0, sizeof(A));
  sprintf(A, "%s%s", tem, B);
  freopen(A, "r", stdin);
  sprintf(A, "%s%s", tem, C);
  freopen(A, "w", stdout);
  return;
}
long long N, tar, sum, val[MAXN], C[MAXN];
vector<long long> q;
int main() {
  fi("");
  scanf("%lld", &N);
  for (long long i = 1; i <= N; i++)
    scanf("%lld", val + i), sum += val[i], C[i] = val[i];
  if (sum == 1) {
    puts("-1");
    return 0;
  }
  if (sum == 0) {
    puts("0");
    return 0;
  }
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      while (sum % i == 0) sum /= i;
      q.push_back(i);
    }
  }
  if (sum > 1) q.push_back(sum);
  long long minn = 0x3f3f3f3f3f3f3f3f;
  for (long long j = 0, k = q.size(); j < k; j++) {
    long long now = 1, ans = 0;
    tar = q[j];
    for (long long i = 1; i <= N; i++) val[i] = C[i] % tar;
    while (now <= N) {
      if (val[now] == 0) {
        now++;
        continue;
      }
      long long nxt = now + 1;
      if (val[now] > 0) val[now] %= tar;
      long long ned = val[now] > 0 ? tar - val[now] : -val[now];
      if (ned <= tar / 2)
        ans += ned * (nxt - now), val[nxt] -= ned, val[now] += ned;
      else
        val[nxt] += val[now], ans += val[now] * (nxt - now), val[now] = 0;
      now++;
    }
    minn = min(ans, minn);
  }
  printf("%lld\n", minn);
  return 0;
}
