#include <bits/stdc++.h>
using namespace std;
const int MX = 3e5 + 69;
int n, kk;
struct node {
  int p, l, c;
} ar[111];
bool cmp(node a, node b) {
  if (a.l != b.l) return a.l < b.l;
  if (a.p != b.p) return a.p < b.p;
  return a.c < b.c;
}
int lvl[111], sex = 3069;
bitset<110000> vis;
bool prim[220200];
vector<int> fuck[1011];
int main() {
  for (int i = 2; i <= 200000; i++) {
    for (int j = i * 2; j <= 200000; j += i) {
      prim[j] = 1;
    }
  }
  cin >> n >> kk;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i].p >> ar[i].c >> ar[i].l;
  }
  sort(ar + 1, ar + 1 + n, cmp);
  for (int i = 1; i <= n; i++) lvl[ar[i].l] = i;
  for (int i = 1; i <= n; i++) {
    if (lvl[i] == 0) continue;
    for (int j = 0; j < sex; j++) {
      random_shuffle(ar + 1, ar + 1 + lvl[i]);
      vis = 0;
      long long zbr = 0;
      vector<int> vv;
      for (int k = 1; k <= lvl[i]; k++) {
        bool ok = 0;
        for (auto u : vv)
          if (prim[u + ar[k].c] ^ 1) ok = 1;
        if (ok) continue;
        zbr += ar[k].p;
        vv.push_back(ar[k].c);
      }
      if (zbr >= kk) {
        cout << i;
        return 0;
      }
    }
  }
  cout << -1;
  return 0;
}
