{"raw_statement":[{"iden":"statement","content":"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).\n\nWhat is the minimum number of operations you need to make all of the elements equal to 1?"},{"iden":"input","content":"The first line of the input contains one integer _n_ (1 ≤ _n_ ≤ 2000) — the number of elements in the array.\n\nThe second line contains _n_ space separated integers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 109) — the elements of the array."},{"iden":"output","content":"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."},{"iden":"examples","content":"Input\n\n5\n2 2 3 4 6\n\nOutput\n\n5\n\nInput\n\n4\n2 4 6 8\n\nOutput\n\n\\-1\n\nInput\n\n3\n2 6 9\n\nOutput\n\n4"},{"iden":"note","content":"In the first sample you can turn all numbers to 1 using the following 5 moves:\n\n*   \\[2, 2, 3, 4, 6\\].\n*   \\[2, 1, 3, 4, 6\\]\n*   \\[2, 1, 3, 1, 6\\]\n*   \\[2, 1, 1, 1, 6\\]\n*   \\[1, 1, 1, 1, 6\\]\n*   \\[1, 1, 1, 1, 1\\]\n\nWe can prove that in this case it is not possible to make all numbers one using less than 5 moves."}],"translated_statement":[{"iden":"statement","content":"你有一个长度为 $n$ 的数组 $a$，你可以执行操作。每个操作如下：选择数组 $a$ 中两个**相邻**的元素，记为 $x$ 和 $y$，并将其中一个替换为 $\\gcd(x, y)$，其中 $\\gcd$ 表示最大公约数。\n\n你需要多少次最少的操作，才能使所有元素都变为 $1$？\n\n输入的第一行包含一个整数 $n$ ($1 ≤ n ≤ 2000$) —— 数组中元素的个数。\n\n第二行包含 $n$ 个空格分隔的整数 $a_1, a_2, ..., a_n$ ($1 ≤ a_i ≤ 10^9$) —— 数组的元素。\n\n如果不可能将所有数字变为 $1$，请输出 _-1_。否则，请输出使所有数字等于 $1$ 所需的最少操作次数。\n\n在第一个样例中，你可以通过以下 $5$ 步操作将所有数字变为 $1$：\n\n我们可以证明，在这种情况下，不可能用少于 $5$ 步操作使所有数字变为 $1$。\n\n"},{"iden":"input","content":"输入的第一行包含一个整数 $n$ ($1 ≤ n ≤ 2000$) —— 数组中元素的个数。第二行包含 $n$ 个空格分隔的整数 $a_1, a_2, ..., a_n$ ($1 ≤ a_i ≤ 10^9$) —— 数组的元素。"},{"iden":"output","content":"如果不可能将所有数字变为 $1$，请输出 _-1_。否则，请输出使所有数字等于 $1$ 所需的最少操作次数。"},{"iden":"examples","content":"输入52 2 3 4 6输出5输入42 4 6 8输出-1输入32 6 9输出4"},{"iden":"note","content":"在第一个样例中，你可以通过以下 $5$ 步操作将所有数字变为 $1$： $[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]$ 我们可以证明，在这种情况下，不可能用少于 $5$ 步操作使所有数字变为 $1$。"}],"sample_group":[],"show_order":[],"formal_statement":"Let $ a = [a_1, a_2, \\dots, a_n] $ be an array of positive integers.\n\nDefine $ g = \\gcd(a_1, a_2, \\dots, a_n) $.  \nIf $ g \\neq 1 $, output $-1$.\n\nOtherwise, let $ k $ be the number of elements in $ a $ equal to $ 1 $.  \nIf $ k > 0 $, then the minimum number of operations is $ n - k $.\n\nIf $ k = 0 $, let $ m $ be the minimum length of a contiguous subarray of $ a $ whose GCD is $ 1 $.  \nThen the minimum number of operations is $ (m - 1) + (n - 1) $.\n\nThat is:  \n$$\n\\text{Answer} = \n\\begin{cases}\n-1 & \\text{if } \\gcd(a_1, \\dots, a_n) \\ne 1 \\\\\nn - k & \\text{if } k > 0 \\\\\n(m - 1) + (n - 1) & \\text{if } k = 0\n\\end{cases}\n$$  \nwhere $ k = |\\{ i : a_i = 1 \\}| $, and $ m = \\min\\{ \\ell : \\exists i \\text{ s.t. } \\gcd(a_i, a_{i+1}, \\dots, a_{i+\\ell-1}) = 1 \\} $.","simple_statement":null,"has_page_source":false}