#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
// Can be used for GCD, LCM, Maximum, Minimum queries, OR this code is for Range Minimum Query AKA RMQ
struct sparseTable
{
int size, LOG;
vector<vector<ll>> m;
ll merge(const ll &a, const ll &b)
{
return gcd(a, b);
}
void build(const vector<ll> &arr)
{
int N = arr.size();
for (int i{}; i < N; i++)
m[i][0] = arr[i];
for (int k = 1; k < LOG; k++)
{
for (int i{}; i + (1 << k) - 1 < N; i++)
m[i][k] = merge(m[i][k - 1], m[i + (1 << (k - 1))][k - 1]);
}
}
sparseTable(const vector<ll> &arr, int lg)
{
int n = arr.size();
LOG = lg;
m.resize(n, vector<ll>(LOG, 0));
build(arr);
}
ll query(int L, int R) // 0-based
{
int len = R - L + 1;
int k = 31 - __builtin_clz(len);
return merge(m[L][k], m[R - (1 << k) + 1][k]);
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("Output.txt", "w", stdout
); #endif //! ONLINE_JUDGE
int t = 1;
ll N;
// cin >> t;
while (t--)
{
cin >> N;
vector<ll> vc(N);
for (int i{}; i < N; i++)
cin >> vc[i];
sparseTable SPT(vc, 17);
int minLen = 0x7fffffff, L{}, R{};
ll g{};
while (R < N)
{
g = gcd(g, vc[R]); // Expand
if (g == 1)
minLen = min(minLen, R - L + 1);
while (g == 1 && L < R) // Shrink
{
minLen = min(minLen, R - L + 1);
L++;
g = SPT.query(L, R);
}
if (g == 1)
minLen = min(minLen, R - L + 1);
R++;
}
if (minLen != INT_MAX)
cout << minLen;
else
cout << -1;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nIGludAojZGVmaW5lIGVuZGwgIlxuIgoKLy8gQ2FuIGJlIHVzZWQgZm9yIEdDRCwgTENNLCBNYXhpbXVtLCBNaW5pbXVtIHF1ZXJpZXMsIE9SI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