#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int M = 3069;
int num[4];
int prime[N], a[N], lx[N], b[N];
struct node {
  int v, c, level;
} p[N];
int cmp(node A, node B) { return A.level < B.level; }
int n, k;
int main() {
  memset(a, 0, sizeof(a));
  int cnt = 0;
  a[0] = a[1] = 1;
  for (int i = 0; i < N; i++) {
    if (a[i]) continue;
    for (int j = 2 * i; j < N; j += i) a[j] = 1;
  }
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d %d %d", &p[i].v, &p[i].c, &p[i].level);
  sort(p + 1, p + n + 1, cmp);
  memset(lx, 0, sizeof(lx));
  for (int i = 1; i <= n; i++) lx[p[i].level] = i;
  int ans = 0, sum, c;
  for (register int i = 1; i <= n; i++) {
    if (!lx[i]) continue;
    if (i == 4) {
      int l;
      l = 0;
    }
    for (register int h = 0; h < M; h++) {
      random_shuffle(p + 1, p + lx[i] + 1);
      c = 0, sum = 0;
      for (register int j = 1; j <= lx[i]; j++) {
        int flag = 0;
        for (int q = 0; q < c; q++)
          if (!a[p[j].c + p[b[q]].c]) {
            flag = 1;
            break;
          }
        if (!flag) {
          b[c++] = j;
          sum += p[j].v;
        }
      }
      if (sum >= k) {
        printf("%d\n", i);
        return 0;
      }
    }
  }
  printf("-1\n");
}
