#include <bits/stdc++.h>
using namespace std;
const int maxd = 1e6 + 10;
int a[maxd];
long long sum, ans = 1e18;
vector<long long> b;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) sum += a[i];
  if (sum == 1) {
    printf("-1\n");
    return 0;
  }
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      b.push_back(i);
      sum /= i;
      i--;
    }
  }
  b.push_back(sum);
  for (auto x : b) {
    long long res = 0;
    sum = 0;
    for (int i = 1; i <= n; i++) {
      sum += a[i];
      sum %= x;
      res += min(sum, x - sum);
    }
    ans = min(res, ans);
  }
  printf("%lld\n", ans);
  return 0;
}
