#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool isDigit[256] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
bool isWhiteSpace[256] = {
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
template <int SIZE>
class ReadStream {
 private:
  FILE* stream;
  char buffer[SIZE];
  char* begin;
  char* end;
  void readBuffer() {
    this->begin = this->buffer;
    this->end =
        this->begin + fread(this->buffer, sizeof(char), SIZE, this->stream);
  }
  char currentChar() { return *this->begin; }
  void advance() {
    this->begin++;
    if (this->begin == this->end) this->readBuffer();
  }

 public:
  ReadStream(FILE* stream = stdin) {
    this->stream = stream;
    this->begin = NULL;
    this->end = NULL;
    this->readBuffer();
  }
  char nextChar() {
    char answer = this->currentChar();
    this->advance();
    return answer;
  }
  int nextUnsignedInt() {
    while (isWhiteSpace[(int)this->currentChar()]) this->advance();
    int answer = 0;
    while (isDigit[(int)this->currentChar()]) {
      answer *= 10;
      answer += this->currentChar() - '0';
      this->advance();
    }
    return answer;
  }
  double nextDouble() {
    double answer = nextUnsignedInt();
    if (this->currentChar() == '.') {
      advance();
      int decimals = 0;
      while (isDigit[(int)this->currentChar()]) {
        answer *= 10;
        answer += this->currentChar() - '0';
        advance();
        decimals++;
      }
      while (decimals > 0) {
        answer /= 10;
        decimals--;
      }
    }
    return answer;
  }
  int nextString(char* answer) {
    while (isWhiteSpace[(int)this->currentChar()]) this->advance();
    int length = 0;
    while (!isWhiteSpace[(int)this->currentChar()]) {
      answer[length] = this->currentChar();
      length++;
      this->advance();
    }
    answer[length] = 0;
    return length;
  }
};
int main() {
  int n;
  scanf("%d", &n);
  ll a[n + 1];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  a[n] = 0;
  if (sum <= 1) {
    printf("-1\n");
  } else {
    vector<ll> divs;
    ll p = 2;
    ll copy = sum;
    while (copy > 1 && p * p <= copy) {
      if (copy % p == 0) {
        divs.push_back(p);
        while (copy % p == 0) {
          copy /= p;
        }
      }
      p++;
    }
    if (copy > 1) {
      divs.push_back(copy);
    }
    std::sort(divs.begin(), divs.end());
    ll answer = LONG_LONG_MAX;
    for (ll d : divs) {
      ll currentAnswer = 0;
      ll currentSum = 0;
      for (int i = 0; i < n && currentAnswer < answer; i++) {
        currentSum += a[i];
        if (currentSum >= d) {
          currentSum %= d;
        }
        if (2 * currentSum <= d) {
          currentAnswer += currentSum;
        } else if (2 * currentSum > d) {
          currentAnswer += d - currentSum;
        }
      }
      answer = min(answer, currentAnswer);
    }
    printf("%lld\n", answer);
  }
  return 0;
}
