#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  vector<long long int> data;
  vector<long long int> arr;
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    arr.push_back(x);
    sum += x;
  }
  for (long long int i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      data.push_back(i);
      while (sum % i == 0) sum /= i;
    }
  }
  if (sum > 1) data.push_back(sum);
  long long int ans = 100000000000000000 * 10 + 10;
  for (long long int j = 0; j < data.size(); j++) {
    long long int x = data[j], cnt = 0;
    sum = 0;
    for (long long int i = 0; i < n; i++) {
      sum += arr[i];
      sum %= x;
      cnt += min(sum, x - sum);
    }
    ans = min(ans, cnt);
  }
  if (ans == 100000000000000000 * 10 + 10)
    cout << -1;
  else
    cout << ans;
}
