#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 1e6 + 7;
struct node {
  long long a, w;
  bool operator<(const node a) const { return w < a.w; }
} c[N];
vector<long long> ok;
long long n, m, K, res, f[12][1 << 11], g[1 << 11];
map<long long, vector<long long>> mp;
void init() {
  n = read(), K = read();
  for (int i = 1; i <= n; i++) {
    c[i].a = read();
    res = gcd(res, c[i].a);
  }
  for (int i = 1; i <= n; i++) c[i].w = read();
  if (res == 1) puts("0"), exit(0);
  for (int i = 2; i <= 1e6 && 1ll * i * i <= res; i++)
    if (res % i == 0) {
      ok.push_back(i);
      while (res % i == 0) res /= i;
    }
  if (res - 1) ok.push_back(res);
  m = ok.size();
  for (int i = 1; i <= n; i++) {
    long long z = 1;
    for (auto j : ok)
      while (c[i].a % j == 0) c[i].a /= j, z *= j;
    mp[z].push_back(c[i].w);
  }
}
void solve() {
  memset(f, 2, sizeof(f));
  f[0][0] = 0;
  for (auto t : mp) {
    long long x = t.first;
    sort(t.second.begin(), t.second.end());
    if (t.second.size() > m) t.second.resize(m);
    vector<long long> a;
    for (int i = 0; i < 1 << m; i++) {
      long long times = 1, p = x;
      for (int j = 0; j < m; j++)
        if (i & (1 << j))
          while (p % ok[j] == 0) p /= ok[j], times *= ok[j];
      a.push_back(times);
    }
    for (auto p : t.second) {
      bool flg = 0;
      for (int i = m - 1; i >= 0; i--)
        for (int j = 0; j < 1 << m; j++)
          if (f[i][j] <= 1e12) {
            int tmp = (1 << m) - 1 - j;
            for (int k = tmp; k; k = (k - 1) & tmp)
              if (a[k] <= K)
                if (f[i + 1][j | k] > f[i][j] + p)
                  f[i + 1][j | k] = f[i][j] + p, flg = 1;
          }
      if (!flg) break;
    }
  }
  long long ans = 1e18;
  for (int i = 1; i <= m; i++) ans = min(ans, f[i][(1 << m) - 1] * i);
  if (ans > 1e12)
    puts("-1");
  else
    printf("%lld\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
