#include <bits/stdc++.h>
using namespace std;
using INT = long long;
int n;
int cnt[1000100];
int id[1000100];
vector<int> adj[1000100];
int dis[1000100];
int flag[1000100];
int runs = 0;
int pr[1000100];
int solve(int u) {
  vector<int> vec;
  queue<int> q;
  q.push(u);
  runs++;
  flag[u] = runs;
  int ans = 0x3f3f3f3f;
  dis[u] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vec.push_back(u);
    for (int v : adj[u]) {
      if (flag[v] != runs) {
        dis[v] = dis[u] + 1;
        flag[v] = runs;
        q.push(v);
      }
    }
  }
  for (int u : vec) {
    int mn0 = 0x3f3f3f3f, mn1 = 0x3f3f3f3f;
    for (int v : adj[u])
      if (dis[v] <= dis[u]) {
        int x = dis[v];
        if (mn1 > x) swap(x, mn1);
        if (mn0 > x) swap(x, mn0);
      }
    if (mn0 != 0x3f3f3f3f) {
      ans = min(ans, mn0 + mn1 + 2);
    }
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 2; i < 1000100; i++)
    if (pr[i] == 0) {
      for (int j = i; j < 1000100; j += i) pr[j] = i;
    }
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d", &u);
    int p = 1;
    while (u > 1) {
      int ss = 0, pp = pr[u];
      while (u % pp == 0) u /= pp, ss++;
      if (ss & 1) p *= pp;
    }
    cnt[p]++;
  }
  if (cnt[1]) return puts("1"), 0;
  for (int i = 1; i < 1000100; i++)
    if (cnt[i] > 1) {
      return puts("2"), 0;
    }
  for (int i = 1; i < 1000100; i++)
    if (cnt[i]) {
      int u = i / pr[i], v = i / u;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
  int ans = n + 1;
  for (int i = 1; i * i < 1000100; i++)
    if (adj[i].size() > 1) {
      ans = min(ans, solve(i));
    }
  if (ans == n + 1)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}
