#include <bits/stdc++.h>
using namespace std;
const int MAXA = 1000100, INF = 0x3f3f3f3f;
int n, ans = INF, lvl[MAXA], p[MAXA];
vector<int> g[MAXA];
void bfs(int v) {
  memset(lvl, INF, sizeof lvl);
  queue<int> q;
  lvl[v] = 0;
  q.push(v);
  while (!q.empty()) {
    v = q.front(), q.pop();
    for (const int &u : g[v])
      if (u != p[v]) {
        if (lvl[u] == INF)
          p[u] = v, lvl[u] = lvl[v] + 1, q.push(u);
        else
          ans = min(ans, lvl[u] + lvl[v] + 1);
      }
  }
}
void f(int x) {
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0) {
      while (x % (i * i) == 0) x /= i * i;
    }
  if (x == 1) ans = 1;
  int p1 = x, p2 = 1;
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0) p1 = x / i, p2 = i;
  g[p1].push_back(p2);
  g[p2].push_back(p1);
}
int main() {
  cin >> n;
  for (int i = 1, x; i <= n; ++i) cin >> x, f(x);
  for (int i = 1; i * i < MAXA; ++i) bfs(i);
  if (ans == INF)
    cout << -1;
  else
    cout << ans << endl;
}
