This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems

Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.

Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes. 

Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^6) — the number of chocolate boxes.

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the number of chocolate pieces in the i-th box.

It is guaranteed that at least one of a_1, a_2, …, a_n is positive.

Output

If there is no way for Charlie to make Alice happy, print -1.

Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.

Examples

Input


3
4 8 5


Output


9


Input


5
3 10 2 1 5


Output


2


Input


4
0 5 15 10


Output


0


Input


1
1


Output


-1

Note

In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by 17.

In the second example, Charlie can move a piece from box 2 to box 3 and a piece from box 4 to box 5. Each box will be divisible by 3.

In the third example, each box is already divisible by 5.

In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
void itval(istream_iterator<string> it) {}
template <typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  itval(++it, args...);
}
const long long int MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {
  cout << x << "\n";
}
template <typename T>
inline void printvec(T x) {
  for (auto a : x) cout << a << ' ';
  cout << '\n';
}
struct custom {
  bool operator()(const pair<int, pair<int, int> > &p1,
                  const pair<int, pair<int, int> > &p2) const {
    if (p1.first == p2.first) return p1.second.first > p2.second.first;
    return p1.first > p2.first;
  }
};
long long int get_pow(long long int a, long long int b, long long int M = MOD) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }
  return res;
}
const long long int N = 1e6 + 5, inf = 2e18;
vector<long long> v;
int n;
long long int calc(long long int p) {
  long long int sum = 0, ans = 0;
  for (long long int i = (long long int)0; i < (long long int)(n); i++) {
    sum += v[i];
    ans += min(sum % p, p - sum % p);
  }
  return ans;
}
void solve() {
  cin >> n;
  v.resize(n);
  long long int sum = 0;
  for (long long int i = (long long int)0; i < (long long int)(n); i++)
    cin >> v[i], sum += v[i];
  if (sum == 1) {
    cout << "-1\n";
    return;
  }
  if (sum == 0) {
    cout << "0\n";
    return;
  }
  long long int i = 2, ans = inf;
  while (i * i <= sum) {
    if (sum % i == 0) {
      ans = min(ans, calc(i));
      while (sum % i == 0) sum = sum / i;
    }
    i++;
  }
  if (sum > 1) ans = min(ans, calc(sum));
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  clock_t z = clock();
  for (long long int tes = (long long int)0; tes < (long long int)(test);
       tes++) {
    solve();
  }
  fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC),
      fflush(stderr);
  return 0;
}
