#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int x, n, ans = INT_MAX;
struct node {
  int nxt, eid;
};
vector<node> edges[maxn];
map<pair<int, int>, int> mp;
bool vis[maxn];
int dis[maxn];
queue<int> q;
void Div(int x, int id) {
  int p = 1, q = 1;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      int num = 0;
      while (x % i == 0) num++, x /= i;
      if (num % 2) {
        if (p == 1)
          p = i;
        else
          q = i;
      }
    }
  }
  if (x != 1) {
    if (p == 1)
      p = x;
    else
      q = x;
  }
  if (p == 1 && q == 1) {
    ans = min(ans, 1);
    return;
  }
  mp[{p, q}]++;
  if (mp[{p, q}] >= 2) {
    ans = min(ans, 2);
    return;
  }
  edges[q].push_back({p, id});
  edges[p].push_back({q, id});
}
int bfs(int s) {
  if (edges[s].empty()) return INT_MAX;
  memset(vis, false, sizeof vis);
  memset(dis, 0, sizeof dis);
  while (!q.empty()) q.pop();
  q.push(s);
  dis[s] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i < edges[now].size(); i++) {
      int nxt = edges[now][i].nxt, eid = edges[now][i].eid;
      if (vis[eid]) continue;
      if (dis[nxt]) return dis[now] + dis[nxt] - 1;
      vis[eid] = true;
      q.push(nxt);
      dis[nxt] = dis[now] + 1;
    }
  }
  return INT_MAX;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    Div(x, i);
  }
  if (ans != INT_MAX)
    printf("%d\n", ans);
  else {
    for (int i = 1; i <= 1000; i++) ans = min(ans, bfs(i));
    if (ans == INT_MAX) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
