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 long power(long long x, unsigned long long y) {
  long temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
long long modpow(long long x, unsigned int y, long long p) {
  long long res = 1;
  x = x % p;
  if (y == 0) return 1;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long exponentMod(long long A, long long B, long long C) {
  if (B == 0) return 1;
  if (A == 0) return 0;
  long y;
  if (B % 2 == 0) {
    y = exponentMod(A, B / 2, C);
    y = (y * y) % C;
  } else {
    y = A % C;
    y = (y * exponentMod(A, B - 1, C) % C) % C;
  }
  return (long long)((y + C) % C);
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int gcdExtended(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
void modInverse(int a, int m) {
  int x, y;
  int g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    cout << "Inverse doesn't exist";
  else {
    int res = (x % m + m) % m;
    cout << "Modular multiplicative inverse is " << res;
  }
}
void SieveOfEratosthenes(int n) {
  bool sieve[n + 1];
  long long cnt = 0;
  memset(sieve, 0, sizeof(sieve));
  for (int p = 2; p * p <= n; p++) {
    if (!sieve[p]) {
      for (int i = 2 * p; i <= n; i += p) sieve[i] = p;
    }
  }
  for (int p = 2; p <= n; p++) {
    if (sieve[p]) cnt++;
  }
  cout << cnt;
}
int phi(unsigned int n) {
  float result = n;
  for (int p = 2; p * p <= n; ++p) {
    if (n % p == 0) {
      while (n % p == 0) n /= p;
      result *= (1.0 - (1.0 / (float)p));
    }
  }
  if (n > 1) result *= (1.0 - (1.0 / (float)n));
  return (int)result;
}
long long floorSqrt(long long x) {
  if (x == 0 || x == 1) return x;
  unsigned long long start = 1, end = x, ans;
  while (start <= end) {
    unsigned long long mid = start + (end - start) / 2;
    if (mid * mid == x) return mid;
    if (mid * mid < x) {
      start = mid + 1;
      ans = mid;
    } else
      end = mid - 1;
  }
  return ans;
}
void start() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int32_t main() {
  start();
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] == 1) cnt++;
  }
  long long x = a[0];
  for (long long i = 1; i < n; i++) {
    x = gcd(x, a[i]);
  }
  if (x != 1) {
    cout << -1 << "\n";
    return 0;
  }
  if (cnt > 0) {
    cout << n - cnt << "\n";
    return 0;
  }
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j < n - 1; j++) {
      a[j] = gcd(a[j], a[j + 1]);
      if (a[j] == 1) {
        cout << i + n - 1;
        return 0;
      }
    }
  }
  cout << "\n";
}
