C. Pride
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacentelements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the 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

最新文章

  1. Coding Kata - 挑战你的“底线”
  2. mac下apache配置,解决It is not safe to rely on the system's timezone settings.
  3. [转]ASP.NET MVC IOC 之AutoFac攻略
  4. Spring回调方法DisposableBean接口
  5. 多线程要点--CLR C#学习笔记
  6. c扩展调用php的函数(调用实现php函数的c函数)
  7. 《C程序设计语言现代方法》第5章 编程题
  8. ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器
  9. ios新特性
  10. ASP.NET页面传值与跳转
  11. 《JS权威指南学习总结--9.5 类和类型》
  12. 情景linux--如何摆脱深路径的频繁切换烦恼?
  13. R︱sparkR的安装与使用、函数尝试笔记、一些案例
  14. appJSON["window"]["navigationBarTextStyle"] 字段需为 black 或 white
  15. Git在已有的分支上新建个人分支开发
  16. nc 使用实例
  17. python中的顺序表
  18. get、post的区别
  19. ARCore中四元数的插值算法实现
  20. django交互vue遇到的问题

热门文章

  1. github+git提交 基础用法
  2. C++ Primer 第2章 变量和基本类型
  3. 批量部署ssh免密登陆
  4. Android Canvas类介绍
  5. Oracle在登陆时被告知用户被锁,如何解决?
  6. hdu 1172 猜数字
  7. Snakes and Ladders LightOJ - 1151( 概率dp+高斯消元)
  8. BZOJ4888 [Tjoi2017]异或和 【树状数组】
  9. BZOJ2125 最短路 【仙人掌最短路】
  10. CF995E Number Clicker 解题报告