#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (b) {
    int c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int n;
int p[300005];
int d[300005];
int q[300005];
queue<int> qq;
vector<int> ds[300005];
bool ok[300005];
set<int> pingo;
int main() {
  memset(p, -1, sizeof(p));
  for (int i = 2, ThxDem = 300005; i < ThxDem; ++i)
    if (p[i] < 0)
      for (int j = i; j < 300005; j += i) p[j] = i;
  for (int i = 2, ThxDem = 300005; i < ThxDem; ++i)
    for (int j = i; j < 300005; j += i) ds[j].push_back(i);
  scanf("%d", &n);
  int g = 0;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
    int a, x = 1;
    scanf("%d", &a);
    while (a > 1) {
      int q = p[a];
      x *= q;
      while (a % q == 0) a /= q;
    }
    a = x;
    g = gcd(g, a);
    for (int x : ds[a]) q[x]++;
    pingo.insert(a);
  }
  for (int i = 2, ThxDem = 300005; i < ThxDem; ++i) {
    int a = i, x = 1;
    vector<int> t;
    while (a > 1) {
      int q = p[a];
      x *= q;
      while (a % q == 0) a /= q;
      t.push_back(q);
    }
    if (x != i) continue;
    int s = 0;
    for (int m = 1, ThxDem = 1 << ((int)(t).size()); m < ThxDem; ++m) {
      int z = 1, w = 0;
      for (int j = 0, ThxDem = ((int)(t).size()); j < ThxDem; ++j) {
        if ((m >> j) & 1) z *= t[j], w++;
      }
      if (w & 1)
        s += q[z];
      else
        s -= q[z];
    }
    ok[i] = s != n;
  }
  memset(d, -1, sizeof(d));
  for (int x : pingo) d[x] = 1, qq.push(x);
  while (!qq.empty()) {
    int x = qq.front();
    qq.pop();
    for (int i : ds[x])
      if (ok[i]) {
        int y = x / i;
        if (d[y] < 0) d[y] = d[x] + 1, qq.push(y);
      }
  }
  printf("%d\n", d[1]);
  return 0;
}
