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.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

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

    static class APride {
        private int[] arr;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            arr = new int[n];
            ArrayList<Integer> patches = new ArrayList<>();
            boolean oneIsPresent = false;
            arr[0] = in.nextInt();
            if (arr[0] == 1) oneIsPresent = true;
            if (arr[0] != 1) patches.add(1);
            int totalGCD = arr[0];
            for (int i = 1; i < n; i++) {
                arr[i] = in.nextInt();
                if (arr[i] == 1) oneIsPresent = true;
                if (arr[i] != 1 && arr[i - 1] != 1)
                    patches.set(patches.size() - 1, patches.get(patches.size() - 1) + 1);
                if (arr[i] != 1 && arr[i - 1] == 1) patches.add(1);
                totalGCD = Util.gcd(totalGCD, arr[i]);
            }
            if (totalGCD != 1) {
                out.println(-1);
                out.flush();
                return;
            }
            if (oneIsPresent) {
                int ans = 0;
                for (int x : patches) ans += x;
                out.println(ans);
                out.flush();
                return;
            }
            SegmentTree st = new SegmentTree(n);
            int minSegmentSize = Integer.MAX_VALUE;
            for (int i = 0; i < n - 1; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (st.query(0, 0, n - 1, i, j) == 1) {
                        minSegmentSize = Math.min(minSegmentSize, j - i + 1);
                    }
                }
            }
            out.println(minSegmentSize - 1 + n - 1);
            out.flush();
        }

        class SegmentTree {
            private int[] st;

            private SegmentTree(int n) {
                int x = (int) Math.ceil(Util.log2(n));
                st = new int[(1 << (x + 1)) - 1];
                buildTree(0, 0, n - 1);
            }

            private void buildTree(int sti, int sts, int ste) {
                if (sts == ste) {
                    st[sti] = arr[sts];
                    return;
                }
                int mid = Util.getMid(sts, ste);
                buildTree(2 * sti + 1, sts, mid);
                buildTree(2 * sti + 2, mid + 1, ste);
                st[sti] = Util.gcd(st[2 * sti + 1], st[2 * sti + 2]);
            }

            private int query(int sti, int sts, int ste, int qs, int qe) {
                if (qe < sts || ste < qs) return -1;
                if (qs <= sts && ste <= qe) return st[sti];
                int mid = Util.getMid(sts, ste);
                int res1 = query(2 * sti + 1, sts, mid, qs, qe);
                int res2 = query(2 * sti + 2, mid + 1, ste, qs, qe);
                if (res1 == -1) return res2;
                if (res2 == -1) return res1;
                return Util.gcd(res1, res2);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }

    static class Util {
        public static int gcd(int a, int b) {
            return b == 0 ? Math.abs(a) : gcd(b, a % b);
        }

        public static int getMid(int a, int b) {
            return (a + b) >> 1;
        }

        public static double log2(int num) {
            return Math.log(num) / Math.log(2);
        }

    }
}

