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.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Asgar Javadov
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        FastWriter out = new FastWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, FastWriter out) {
            int n = in.nextInt();
            int[] a = in.readIntArray(n);
            int gcd = a[0];
            for (int i = 1; i < n; ++i)
                gcd = NumberTheory.gcd(gcd, a[i]);
            if (gcd > 1) {
                out.println(-1);
                return;
            }

            if (n == 1) {
                out.println(0);
                return;
            }

            int ans = 0;
            int m = MathUtils.min(a);
            if (m == 1) {
                for (int i = 0; i < a.length; ++i) {
                    if (a[i] != 1) {
                        ++ans;
                    }
                }
                out.println(ans);
                return;
            }

            int M = Integer.MAX_VALUE;
            int[] b = Arrays.copyOf(a, a.length);
            for (int j = 0; j < n; ++j) {
                ans = 0;
                a = Arrays.copyOf(b, b.length);
                for (int i = j; i < n - 1; ++i) {
                    int g = NumberTheory.gcd(a[i], a[i + 1]);
                    if (a[i + 1] > g) {
                        a[i + 1] = g;
                        ++ans;
                    }
                    if (g == 1) {
                        ans += n - 1;
                        M = MathUtils.min(M, ans);
                        break;
                    }
                }
            }
            out.println(M);
        }

    }

    static class InputReader extends BufferedReader {
        StringTokenizer tokenizer;

        public InputReader(InputStream inputStream) {
            super(new InputStreamReader(inputStream), 32768);
        }

        public InputReader(String filename) {
            super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(readLine());
                } catch (IOException e) {
                    throw new RuntimeException();
                }
            }
            return tokenizer.nextToken();
        }

        public Integer nextInt() {
            return Integer.valueOf(next());
        }

        public int[] readIntArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
                array[i] = nextInt();
            return array;
        }

    }

    static class FastWriter extends PrintWriter {
        public FastWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public FastWriter(Writer writer) {
            super(writer);
        }

        public FastWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

    }

    static class MathUtils {
        public static int min(int... args) {
            int res = args[0];
            for (int i = 1; i < args.length; ++i)
                if (res > args[i])
                    res = args[i];
            return res;
        }

    }

    static class NumberTheory {
        public static int gcd(int a, int b) {
            return (b == 0) ? a : gcd(b, a % b);
        }

    }
}

