intmain() { std::cin >> n >> lim; for (int i = 1; i <= n; ++ i) scanf("%lld", a + i); for (int i = 1; i <= n; ++ i) scanf("%d", c + i); G = a[1]; for (int i = 2; i <= n; ++ i) G = Gcd(G, a[i]); LL tmp = G; for (int i = 2; i <= tmp / i; ++ i) { if (tmp % i) continue; while (tmp % i == 0) tmp /= i; fac.push_back(i); } if (tmp ^ 1) fac.push_back(tmp); int tot = fac.size(); for (int i = 1; i <= n; ++ i) { LL x = 1; for (LL p : fac) while (a[i] % p == 0) x *= p, a[i] /= p; mp[a[i] = x].push_back(i); } for (auto pr : mp) { std::sort(pr.second.begin(), pr.second.end(), cmp); if (pr.second.size() > fac.size()) pr.second.resize(fac.size()); for (int id : pr.second) { dv[0] = 1; LL cur = a[id]; for (int i = 0; i < tot; ++ i) { dv[1 << i] = 1; while (cur % fac[i] == 0) cur /= fac[i], dv[1 << i] *= fac[i]; } for (int s = 1; s < (1 << tot); ++ s) { if ((dv[s] = dv[s & (s - 1)] * dv[s & -s]) > lim) continue; cov[s].push_back(id); } } } for (int s = 1; s < (1 << tot); ++ s) { std::sort(cov[s].begin(), cov[s].end(), cmp); if (cov[s].size() > fac.size()) cov[s].resize(fac.size()); // std::cout << s << " : "; // for (int id : cov[s]) std::cout << id << ' '; // std::cout << '\n'; for (int id : cov[s]) trs[id].push_back(s); } for (int s = 0; s < (1 << tot); ++ s) for (int i = 0; i <= tot; ++ i) g[i][s] = f[i][s] = INF; f[0][0] = 0; for (int id = 1; id <= n; ++ id) { if (!trs[id].size()) continue; for (int s : trs[id]) { int t = ((1 << tot) - 1) ^ s, all = t; do { for (int i = 0; i < tot; ++ i) chkmin(g[i + 1][s | t], f[i][t] + c[id]); // std::cout << id << ' ' << s << ' ' << t << '\n'; t = (t - 1) & all; } while (t != all); } for (int s : trs[id]) { int t = ((1 << tot) - 1) ^ s, all = t; do { for (int i = 1; i <= tot; ++ i) chkmin(f[i][s | t], g[i][s | t]), g[i][s | t] = INF; t = (t - 1) & all; } while (t != all); } } LL res = INF; for (int i = 0; i <= tot; ++ i) if (f[i][(1 << tot) - 1] != INF) chkmin(res, f[i][(1 << tot) - 1] * i); std::cout << (res == INF ? -1 : res) << '\n'; return0; }