#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long a = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return a;
}
inline long long gcd(long long a, long long b) {
  long long r = a % b;
  while (r) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
const long long MAXN = 1e6 + 7;
struct zt {
  long long num, w;
  bool operator<(const zt a) const { return w < a.w; }
} now[MAXN];
vector<long long> in;
long long N, K, allGcd, dp[12][1 << 11], calc[1 << 11];
map<long long, vector<long long> > lsh;
signed main() {
  N = read();
  K = read();
  for (long long i = 1; i <= N; ++i) {
    now[i].num = read();
    allGcd = gcd(allGcd, now[i].num);
  }
  for (long long i = 1; i <= N; ++i) now[i].w = read();
  if (allGcd == 1) return puts("0"), 0;
  for (long long i = 2; i <= 1e6 && 1ll * i * i <= allGcd; ++i)
    if (allGcd % i == 0) {
      in.push_back(i);
      while (allGcd % i == 0) allGcd /= i;
    }
  if (allGcd - 1) in.push_back(allGcd);
  long long M = in.size();
  for (long long i = 1; i <= N; ++i) {
    long long z = 1;
    for (auto j : in)
      while (now[i].num % j == 0) {
        now[i].num /= j;
        z *= j;
      }
    lsh[z].push_back(now[i].w);
  }
  memset(dp, 2, sizeof(dp));
  dp[0][0] = 0;
  for (auto t : lsh) {
    long long x = t.first;
    sort(t.second.begin(), t.second.end());
    if (t.second.size() > M) t.second.resize(M);
    vector<long long> num;
    for (long long i = 0; i < 1 << M; ++i) {
      long long times = 1, p = x;
      for (long long j = 0; j < M; ++j)
        if (i & (1 << j))
          while (p % in[j] == 0) {
            p /= in[j];
            times *= in[j];
          }
      num.push_back(times);
    }
    for (auto p : t.second) {
      bool flg = 0;
      for (long long i = M - 1; i >= 0; --i)
        for (long long j = 0; j < 1 << M; ++j)
          if (dp[i][j] <= 1e12) {
            long long all = (1 << M) - 1 - j;
            for (long long k = all; k; k = (k - 1) & all)
              if (num[k] <= K)
                if (dp[i + 1][j | k] > dp[i][j] + p) {
                  dp[i + 1][j | k] = dp[i][j] + p;
                  flg = 1;
                }
          }
      if (!flg) break;
    }
  }
  long long minN = 1e18;
  for (long long i = 1; i <= M; ++i) minN = min(minN, dp[i][(1 << M) - 1] * i);
  if (minN > 1e12)
    cout << -1;
  else
    cout << minN;
  return 0;
}
