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>
using namespace std;
long double pi = 3.14159265358979323846;
vector<long long> al[500005];
long long vis[500005], color[500005], I[101][101];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long bpow(long long n, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po % 2) {
      res *= n;
      po--;
    } else {
      n *= n;
      po /= 2;
    }
  }
  return res;
}
long long binpow(long long n, long long po, long long mod) {
  long long res = 1;
  while (po > 0) {
    if (po % 2) {
      res = (res * n) % mod;
      po--;
    } else {
      n = (n * n) % mod;
      po /= 2;
    }
  }
  return res;
}
long long swap(long long *a, long long *b) {
  long long temp = *a;
  *a = *b;
  *b = temp;
}
long long fact(long long n) {
  if ((n == 0) || (n == 1))
    return 1;
  else
    return n * fact(n - 1);
}
long long C(long long n, long long r) {
  if (r > n - r) r = n - r;
  long long ans = 1;
  long long i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
    ans = ans % 1000000007;
  }
  return ans;
}
void mmul(long long A[][101], long long B[][101], long long dim) {
  long long res[dim][dim];
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) {
      res[i][j] = 0;
      for (long long k = 0; k < dim; k++)
        res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % 1000000007;
    }
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) A[i][j] = res[i][j];
}
void mpow(long long A[][101], long long dim, long long po) {
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) {
      if (i == j)
        I[i][j] = 1;
      else
        I[i][j] = 0;
    }
  while (po > 0) {
    if (po % 2 == 1) {
      mmul(I, A, dim);
      po--;
    } else {
      mmul(A, A, dim);
      po /= 2;
    }
  }
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) A[i][j] = I[i][j];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long testcases = 1;
  while (testcases--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long ans = 100000000;
    long long g = a[0];
    long long ones = 0;
    for (long long i = 1; i < n; i++) {
      g = gcd(g, a[i]);
    }
    for (long long i = 0; i < n; i++)
      if (a[i] == 1) ones++;
    if (g != 1)
      cout << -1;
    else if (ones == 0) {
      for (long long i = 0; i < n; i++) {
        g = a[i];
        long long count = 0;
        for (long long j = i + 1; j < n; j++) {
          g = gcd(g, a[j]);
          count++;
          if (g == 1) break;
        }
        if (g == 1) ans = min(ans, count);
        g = a[i];
        count = 0;
        for (long long j = i - 1; j >= 0; j--) {
          g = gcd(g, a[j]);
          count++;
          if (g == 1) break;
        }
        if (g == 1) ans = min(ans, count);
      }
      cout << ans + n - 1;
    } else
      cout << n - ones;
  }
}
