#include <bits/stdc++.h>
using namespace std;
const int Maxn = 3e5 + 10;
const int lim = 1000;
int prime[Maxn], M[Maxn], H[Maxn], cnt;
bool vis[Maxn];
struct node {
  int v, c, l;
} a[Maxn];
int cmp(node A, node B) { return A.l < B.l; }
int n, k;
int main() {
  int cnt = 0;
  vis[0] = vis[1] = 1;
  for (int i = 2; i <= 3e5; i++) {
    if (!vis[i]) prime[++cnt] = i;
    for (int j = 1; j <= cnt && i * prime[j] <= 3e5; j++) {
      vis[i * prime[j]] = 1;
      if (i % prime[j] == 0) break;
    }
  }
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d %d %d", &a[i].v, &a[i].c, &a[i].l);
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) M[a[i].l] = i;
  int ans = 0, sum, top;
  for (int i = 1; i <= n; i++) {
    if (!M[i]) continue;
    for (int h = 0; h < lim; h++) {
      random_shuffle(a + 1, a + M[i] + 1);
      top = 0, sum = 0;
      for (int j = 1; j <= M[i]; j++) {
        bool _ = 1;
        for (int q = 0; q < top; q++)
          if (!vis[a[j].c + a[H[q]].c]) {
            _ = 0;
            break;
          }
        if (_) {
          H[top++] = j;
          sum += a[j].v;
        }
      }
      if (sum >= k) return 0 * printf("%d\n", i);
    }
  }
  puts("-1");
}
