#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> maxi[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > v) swap(u, v);
    if (v > 1000) {
        maxi[v].emplace_back(u);
        if (!pos[u]) pos[u] = ++tot, node[tot] = u;
        return;
    }
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (g[i][j] == 0 && i != j) g[i][j] = INF;
    for (int t = 1; t < N; t++)
        for (int i = 0; i < SZ(maxi[t]); i++)
            for (int j = 0; j < SZ(maxi[t]); j++) {
                if (i == j) continue;
                int u = maxi[t][i];
                int v = maxi[t][j];
                if (u == v) {
                    ans = min(ans, 2ll);
                    continue;
                }
                if (g[pos[u]][pos[v]] < INF) {
                    ans = min(ans, g[pos[u]][pos[v]] + 2);
                    continue;
                }
                g[pos[u]][pos[v]] = 2;
            }
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + min(INF, g[i][k] + g[k][j]));  // 更新答案
        for (int i = 1; i <= tot; ++i)
            for (int j = 1; j <= tot; ++j) {
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
            }
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > 1e3 || v > 1e3) return;
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (!g[i][j]) g[i][j] = INF;
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + g[i][k] + g[k][j]);  // 更新答案
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) ps.fi = p;
        }
        if (x != 1) ps.se = x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > 1000 || v > 1000) return;
    //cout << u << " " << v << "\n";
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    if (tot > 100) {
        cout << tot << "\n";
        return;
    }
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (!g[i][j]) g[i][j] = INF;
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + g[i][k] + g[k][j]);  // 更新答案
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> maxi[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > v) swap(u, v);
    if (v > 1000) {
        maxi[v].emplace_back(u);
        if (!pos[u]) pos[u] = ++tot, node[tot] = u;
        return;
    }
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (g[i][j] == 0 && i != j) g[i][j] = INF;
    for (int t = 1; t < N; t++)
        for (int i = 0; i < SZ(maxi[t]); i++)
            for (int j = 0; j < SZ(maxi[t]); j++) {
                if (i == j) continue;
                int u = maxi[t][i];
                int v = maxi[t][j];
                if (u == v) {
                    ans = min(ans, 2ll);
                    continue;
                }
                if (g[pos[u]][pos[v]] < INF) {
                    ans = min(ans, g[pos[u]][pos[v]] + 2);
                    continue;
                }
                g[pos[u]][pos[v]] = min(g[pos[u]][pos[v]], 2ll);
            }
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + min(INF, g[i][k] + g[k][j]));  // 更新答案
        for (int i = 1; i <= tot; ++i)
            for (int j = 1; j <= tot; ++j) {
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
            }
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> maxi[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > v) swap(u, v);
    if (v > 1000) {
        assert(prime[v]);
        maxi[v].emplace_back(u);
        if (!pos[u]) pos[u] = ++tot, node[tot] = u;
        return;
    }
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    if (g[pos[u]][pos[v]] == 1)
        ans = min(ans, 2ll);
    g[pos[u]][pos[v]] = 1;
    g[pos[v]][pos[u]] = 1;
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (g[i][j] == 0 && i != j) g[i][j] = INF;
    for (int t = 1; t < N; t++) {
        for (int i = 0; i < SZ(maxi[t]); i++)
            for (int j = i+1; j < SZ(maxi[t]); j++) {
                int u = maxi[t][i];
                int v = maxi[t][j];
                if (u == v) {
                    ans = min(ans, 2ll);
                    continue;
                }
                if (g[pos[u]][pos[v]] < INF) {
                    if (n == 77829) assert(false);
                    ans = min(ans, g[pos[u]][pos[v]] + 2);
                    continue;
                }
                g[pos[u]][pos[v]] = min(g[pos[u]][pos[v]], 2ll);
                g[pos[v]][pos[u]] = min(g[pos[v]][pos[u]], 2ll);
            }
    }
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + min(INF, g[i][k] + g[k][j]));  // 更新答案
        for (int i = 1; i <= tot; ++i)
            for (int j = 1; j <= tot; ++j) {
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
            }
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> maxi[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > v) swap(u, v);
    if (v > 1000) {
        if (u > 1)
            maxi[v].emplace_back(u);
        return;
    }
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (g[i][j] == 0 && i != j) g[i][j] = INF;
    for (int t = 1; t < N; t++)
        for (int i = 0; i < SZ(maxi[t]); i++)
            for (int j = 0; j < SZ(maxi[t]); j++) {
                if (i == j) continue;
                int u = maxi[t][i];
                int v = maxi[t][j];
                if (u == v) {
                    ans = min(ans, 2ll);
                    return;
                }
                if (g[pos[u]][pos[v]] < INF) {
                    ans = min(ans, 2ll);
                    return;
                }
                g[pos[u]][pos[v]] = 2;
            }
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + min(INF, g[i][k] + g[k][j]));  // 更新答案
        for (int i = 1; i <= tot; ++i)
            for (int j = 1; j <= tot; ++j) {
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
            }
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> primes;
int ans = INF;
int vis[M][M];

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (!g[i][j]) g[i][j] = INF;
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + g[i][k] + g[k][j]);  // 更新答案
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) ps.fi = p;
        }
        if (x != 1) ps.se = x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<LL, LL>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 3e3+6 ;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n;
int x, tot;
int pos[N];
int node[N];
int g[M][M];
bool prime[N];
vector<int> maxi[N];
vector<int> primes;
int ans = INF;

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i < N; i++) {
        if (!prime[i]) continue;
        primes.emplace_back(i);
        for (int j = i*i; j < N; j += i)
            prime[j] = false;
    }
}

void addEdge(int u, int v) {
    if (u > v) swap(u, v);
    if (v > 1000) {
        if (u > 1)
            maxi[v].emplace_back(u);
        if (!pos[u]) pos[u] = ++tot, node[tot] = u;
        return;
    }
    if (!pos[u]) pos[u] = ++tot, node[tot] = u;
    if (!pos[v]) pos[v] = ++tot, node[tot] = v;
    g[pos[u]][pos[v]]++;
    g[pos[v]][pos[u]]++;
    if (g[pos[u]][pos[v]] > 1)
        ans = min(ans, 2ll);
}

void floyd() {
    for (int i = 1; i <= tot; i++)
        for (int j = 1; j <= tot; j++)
            if (g[i][j] == 0 && i != j) g[i][j] = INF;
    for (int t = 1; t < N; t++)
        for (int i = 0; i < SZ(maxi[t]); i++)
            for (int j = 0; j < SZ(maxi[t]); j++) {
                if (i == j) continue;
                int u = maxi[t][i];
                int v = maxi[t][j];
                if (u == v) {
                    ans = min(ans, 2ll);
                    return;
                }
                if (g[pos[u]][pos[v]] < INF) {
                    ans = min(ans, 3ll);
                    return;
                }
                g[pos[u]][pos[v]] = 2;
            }
    static int dis[M][M];  // 最短路矩阵
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= tot; ++j) dis[i][j] = g[i][j];  // 初始化最短路矩阵
    for (int k = 1; k <= tot; ++k) {
        for (int i = 1; i < k; ++i)
            for (int j = 1; j < i; ++j)
                ans = min(ans, dis[i][j] + min(INF, g[i][k] + g[k][j]));  // 更新答案
        for (int i = 1; i <= tot; ++i)
            for (int j = 1; j <= tot; ++j) {
                dis[i][j] = std::min(
                        dis[i][j], dis[i][k] + dis[k][j]);  // 正常的 floyd 更新最短路矩阵
            }
    }
}

signed main() {
    fastio;
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        pii ps = {1, 1};
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.fi = p;
                break;
            }
        }
        if (ps.fi == 1 && x != 1)
            ps.fi = x, x /= x;
        for (int p: primes) {
            if (p * p > x) break;
            int c = 0;
            if (x % p == 0) {
                while (x % p == 0) x /= p, c++;
            }
            if (c & 1) {
                ps.se = p;
                break;
            }
        }
        if (ps.se == 1 && x != 1)
            ps.se = x, x /= x;
        if (ps.fi == 1 && ps.se == 1)
            ans = 1;
        else
            addEdge(ps.fi, ps.se);
    }
    if (ans != INF) cout << ans << "\n";
    else floyd(), cout << (ans < INF ? ans : -1) << "\n";
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    clock_t times=clock();
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(1337);
    while (clock()-times<=2800) {
        shuffle(all(haha),r);
        used=vi(c.size(),0);
        for (int i = 0; i < c.size(); i++) {
            if (used[haha[i]] == 0) {
                dfs(haha[i], haha[i]);
            }
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    clock_t times=clock();
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(228);
    while (clock()-times<=2800) {
        shuffle(all(haha),r);
        used=vi(c.size(),0);
        for (int i = 0; i < c.size(); i++) {
            if (used[haha[i]] == 0) {
                dfs(haha[i], haha[i]);
            }
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    clock_t times=clock();
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(1337);
    while (clock()-times<=2500) {
        shuffle(all(haha),r);
        used=vi(c.size(),0);
        for (int i = 0; i < c.size(); i++) {
            if (used[haha[i]] == 0) {
                dfs(haha[i], haha[i]);
            }
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[i]==0) {
            dfs(i,i);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    clock_t times=clock();
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(1337);
    while (clock()-times<=2800) {
        shuffle(all(haha),r);
        used=vi(c.size(),0);
        for (int i = 0; i < c.size(); i++) {
            if (used[haha[i]] == 0) {
                dfs(haha[i], haha[i]);
            }
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(100);
    shuffle(all(haha),r);
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(228);
    shuffle(all(haha),r);
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    clock_t times=clock();
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(228);
    while (clock()-times>=2500) {
        shuffle(all(haha),r);
        used=vi(c.size(),0);
        for (int i = 0; i < c.size(); i++) {
            if (used[haha[i]] == 0) {
                dfs(haha[i], haha[i]);
            }
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    random_shuffle(all(haha));
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    freopen("a.in","r",stdin);
#endif

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(15);
    shuffle(all(haha),r);
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(1245);
    shuffle(all(haha),r);
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
int ans=INF;
vvi g;
int par[MAXN];
vector <int> used;

void dfs(int s,int p)
{
    used[s]=1;
    par[s]=p;
//    cout<<p<<" "<<s<<'\n';
    for (auto to:g[s])
    {
        if (to==p)
            continue;
        if (used[to]==1)
        {
            int cnt=0;
            int x=p;
            //cout<<to<<" "<<s<<'\n';
            while (x!=to) {
                cnt++, x = par[x];
            }
            cnt++;
            ans=min(ans,cnt+1);
          //      return ;
        }
        else {
            if (used[to]==0)
            dfs(to, s);
        }
    }
    used[s]=2;
}
int main() {
//    ios_base::sync_with_stdio(0);
   // cin.tie(0);
    #ifdef LOCAL
    freopen("a.in","r",stdin);
    #endif
    int n;
    cin>>n;
    vector <int> a(n);
    rep(i,n)
    cin>>a[i];
    vector <pair <int,int> > b(n,{-1,-1});
    rep(i,n)
    {
        b[i]={1,1};
        int cnt=-1,cnt1=-1;
        for (int j=2;j*j<=a[i];j++)
        {
         //   cout<<cnt<<" "<<cnt1<<'\n';
            if (a[i]%j==0)
            {
             //   cout<<j<<'\n';
                if (cnt==-1)
                {
                    cnt=0;
                    while (a[i]%j==0)
                        cnt++,a[i]/=j;
                    if (cnt%2==1)
                        b[i].fi=j;
                  //  cout<<b[i].fi<<'\n';
                  //  cout<<cnt<<" "<<cnt1<<'\n';
                }
                else
                {
                    cnt1=0;
                    while (a[i]%j==0)
                        cnt1++,a[i]/=j;
                    if (cnt1%2==1)
                        b[i].se=j;
                  //  cout<<b[i].se<<'\n';
                }
            }
        }
        if (a[i]>1)
            b[i].se=a[i];
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi>b[i].se)
            swap(b[i].fi,b[i].se);
    }
    for (int i=0;i<n;i++)
    {
        if (b[i].fi==1&&b[i].se==1)
        {
            cout<<1;
            return 0;
        }
    }
  //  rep(i,n)
 //   {
  //      cout<<b[i].fi<<" "<<b[i].se<<'\n';
  //  }
    map <pair <int,int>,int> m;
    for (int i=0;i<n;i++)
    {
        if (m[b[i]]!=0)
        {
            cout<<2;
            return 0;
        }
        else
            m[b[i]]=1;
    }
    set <int> c;
    map <int,int> mm;
    for (int i=0;i<n;i++)
    {
        c.insert(b[i].fi);
        c.insert(b[i].se);
    }
    vector <int> d(c.size());
    int cur=0;
    for (auto to:c) {
        mm[to]=cur;
        d[cur++] = to;
    }
    //cout<<mm[2]<<" "<<mm[3]<<" "<<mm[5]<<'\n';
    g=vvi(d.size());
    for (int i=0;i<n;i++)
    {
        g[mm[b[i].fi]].pb(mm[b[i].se]);
        g[mm[b[i].se]].pb(mm[b[i].fi]);
    }
//    for (int i=0;i<c.size();i++)
//    {
//        cout<<i<<'\n';
//        for (auto to:g[i])
//            cout<<to<<" ";
//        cout<<'\n';
//    }
   // cout<<"aha";
   vector <int> haha(c.size());
    rep(i,c.size())
    haha[i]=i;
    mt19937 r(123);
    shuffle(all(haha),r);
    used=vi(c.size(),0);
    for (int i=0;i<c.size();i++)
    {
        if (used[haha[i]]==0) {
            dfs(haha[i],haha[i]);
        }
    }
    if (ans==INF)
    {
        cout<<-1;
        return 0;
    } else
        cout<<ans;

}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N], ver;
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;
map <int, int> ma[2000];

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				ver.pb(x);
				if(!ma[p[i]].count(x))
				    G.pb(mp(mp(p[i], x), 1)), ma[p[j]][x] = 1;
				if(!ma[p[j]].count(x))
				    G.pb(mp(mp(p[j], x), 1)), ma[p[j]][x] = 1;
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j : ver)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
		v.pb(mp(1000000, p[i]));
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	ans = inf;
	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		if(n != 77829)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N], ver;
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				ver.pb(x);
				G.pb(mp(mp(p[i], x), 1));
				G.pb(mp(mp(p[j], x), 1));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j : ver)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		cout << p1 << " " << p2 << endl;
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(0, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, d[0][i].f + d[1][i].f);

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, D[y] + G[i].s);
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}