Make It One

Janusz is a businessman. He owns a company “Januszex”, which produces games for teenagers. Last hit of Januszex was a cool one-person game “Make it one”. The player is given a sequence of $n$ integers $a_i$.

It is allowed to select any subset of them, and the score is equal to the greatest common divisor of selected elements. The goal is to take as little elements as it is possible, getting the score $1$. Now Janusz wonders, for given sequence, how much elements should the player choose?

Input

The first line contains an only integer $n$ ($1 \le n \le 300\,000$) — the number of integers in the sequence.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 300\,000$).

Output

If there is no subset of the given sequence with gcd equal to $1$, output -1.

Otherwise, output exactly one integer — the size of the smallest subset with gcd equal to $1$.

Examples

input

3
10 6 15

output

3

input

3
2 4 6

output

-1

input

7
30 60 21 42 70 15 30

output

3

Note

In the first example, selecting a subset of all numbers gives a gcd of $1$ and for all smaller subsets the gcd is greater than $1$.

In the second example, for all subsets of numbers the gcd is at least $2$.

Solution:

#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
return '"' + s + '"';
}

string to_string(const char* s) {
return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  const int MAX = 300010;
  vector<int> p(MAX);
iota(p.begin(), p.end(), 0);
for (int i = 2; i < MAX; i++) {
    if (p[i] == i) {
      for (int j = i + i; j < MAX; j += i) {
        if (p[j] == j) {
          p[j] = i;
        }
      }
    }
  }
  int n;
  cin >> n;
vector<int> has(MAX, 0);
vector<vector<int>> d(n);
int g = 0;
for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
g = __gcd(g, x);
has[x] = 1;
while (x > 1) {
if (d[i].empty() || d[i].back() != p[x]) {
d[i].push_back(p[x]);
}
x /= p[x];
}
}
if (g > 1) {
cout << -1 << '\n';
    return 0;
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = i + i; j < MAX; j += i) {
      has[i] += has[j];
    }
  }
  int ans = n + 1;
  for (int i = 0; i < n; i++) {
    int sz = (int) d[i].size();
    vector<int> cnt(1 << sz);
    for (int t = 0; t < (1 << sz); t++) {
      int u = 1;
      for (int j = 0; j < sz; j++) {
        if (t & (1 << j)) {
          u *= d[i][j];
        }
      }
      cnt[t] = has[u];
    }
    for (int bit = 0; bit < sz; bit++) {
      for (int t = 0; t < (1 << sz); t++) {
        if (t & (1 << bit)) {
          cnt[t ^ (1 << bit)] -= cnt[t];
        }
      }
    }
    for (int bit = 0; bit < sz; bit++) {
      for (int t = 0; t < (1 << sz); t++) {
        if (t & (1 << bit)) {
          cnt[t] += cnt[t ^ (1 << bit)];
        }
      }
    }
    vector<int> dp(1 << sz);
    dp[0] = 1;
    for (int t = 1; t < (1 << sz); t++) {
      dp[t] = n + 1;
      int u = t;
      while (u > 0) {
if (cnt[(1 << sz) - 1 - u] > 0) {
dp[t] = min(dp[t], dp[t - u] + 1);
}
u = (u - 1) & t;
}
}
ans = min(ans, dp[(1 << sz) - 1]);
  }
  cout << ans << '\n';
  return 0;
}