You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).

What is the minimum number of operations you need to make all of the elements equal to 1?

Input

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.

The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

Examples

Input

5
2 2 3 4 6


Output

5


Input

4
2 4 6 8


Output

-1


Input

3
2 6 9


Output

4

Note

In the first sample you can turn all numbers to 1 using the following 5 moves:

  * [2, 2, 3, 4, 6]. 
  * [2, 1, 3, 4, 6]
  * [2, 1, 3, 1, 6]
  * [2, 1, 1, 1, 6]
  * [1, 1, 1, 1, 6]
  * [1, 1, 1, 1, 1]



We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
stringstream output;
inline void eOP();
using namespace std;
template <typename N>
inline N euclideanAlgorithm(N a, N b);
template <typename N>
inline N euclideanAlgorithm(N a, N b, N& x, N& y);
template <typename N>
inline N lcm(N a, N b);
inline size_t reverseBits(size_t n);
template <typename N>
inline N euclideanAlgorithm(N a, N b) {
  while (a != N(0)) {
    b %= a;
    swap(a, b);
  }
  return b;
}
template <typename N>
inline N euclideanAlgorithm(N a, N b, N& x, N& y) {
  N l[3] = {a, 1, 0};
  N r[3] = {b, 0, 1};
  while (l[0] != 0) {
    N n = r[0] / l[0];
    for (int i = 0; i < 3; ++i) {
      r[i] -= l[i] * n;
      swap(l[i], r[i]);
    }
  }
  x = r[1];
  y = r[2];
  return r[0];
}
template <typename N>
inline N lcm(N a, N b) {
  return a / euclideanAlgorithm(a, b) * b;
}
inline size_t reverseBits(size_t x) {
  unsigned int tmp1 = (x & 0xAAAAAAAA) >> 1;
  unsigned int tmp2 = (x & 0x55555555) << 1;
  size_t n = tmp1 | tmp2;
  tmp1 = (n & 0xCCCCCCCC) >> 2;
  tmp2 = (n & 0x33333333) << 2;
  n = tmp1 | tmp2;
  tmp1 = (n & 0xF0F0F0F0) >> 4;
  tmp2 = (n & 0x0F0F0F0F) << 4;
  n = tmp1 | tmp2;
  tmp1 = (n & 0xFF00FF00) >> 8;
  tmp2 = (n & 0x00FF00FF) << 8;
  n = tmp1 | tmp2;
  tmp1 = (n & 0xFFFF0000) >> 16;
  tmp2 = (n & 0x0000FFFF) << 16;
  n = tmp1 | tmp2;
  return n;
}
int main() {
  int n;
  cin >> n;
  int* a = new int[n];
  int one = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] == 1) {
      ++one;
    }
  }
  if (one) {
    output << n - one;
    eOP();
  }
  int g = 0;
  for (int i = 0; i < n; ++i) {
    g = euclideanAlgorithm(g, a[i]);
  }
  if (g != 1) {
    output << -1;
    eOP();
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= n - i; ++j) {
      a[j - 1] = euclideanAlgorithm(a[j - 1], a[j]);
      if (a[j - 1] == 1) {
        output << n + i - 1;
        eOP();
      }
    }
  }
  eOP();
}
inline void eOP() {
  cout << output.str();
  output.str(string());
  exit(0);
}
