#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 200;
const int maxm = 30000;
const int MAXN = 2e5 + 5;
bool isprime[MAXN];
struct G {
  int v, cap, next;
  G() {}
  G(int v, int cap, int next) : v(v), cap(cap), next(next) {}
} E[maxm];
int p[maxn], T, n, k;
int d[maxn], temp_p[maxn], qw[maxn];
void init() {
  memset(p, -1, sizeof(p));
  T = 0;
}
void add(int u, int v, int cap) {
  E[T] = G(v, cap, p[u]);
  p[u] = T++;
  E[T] = G(u, 0, p[v]);
  p[v] = T++;
}
bool bfs(int st, int en, int n) {
  int i, u, v, head, tail;
  for (i = 0; i <= n; i++) d[i] = -1;
  head = tail = 0;
  d[st] = 0;
  qw[tail] = st;
  while (head <= tail) {
    u = qw[head++];
    for (i = p[u]; i + 1; i = E[i].next) {
      v = E[i].v;
      if (d[v] == -1 && E[i].cap > 0) {
        d[v] = d[u] + 1;
        qw[++tail] = v;
      }
    }
  }
  return (d[en] != -1);
}
int dfs(int u, int en, int f) {
  if (u == en || f == 0) return f;
  int flow = 0, temp;
  for (; temp_p[u] + 1; temp_p[u] = E[temp_p[u]].next) {
    G& e = E[temp_p[u]];
    if (d[u] + 1 == d[e.v]) {
      temp = dfs(e.v, en, min(f, e.cap));
      if (temp > 0) {
        e.cap -= temp;
        E[temp_p[u] ^ 1].cap += temp;
        flow += temp;
        f -= temp;
        if (f == 0) break;
      }
    }
  }
  return flow;
}
int dinic(int st, int en, int n) {
  int i, ans = 0;
  while (bfs(st, en, n)) {
    for (i = 0; i <= n; i++) temp_p[i] = p[i];
    ans += dfs(st, en, inf);
  }
  return ans;
}
struct node {
  int p, c, l;
  void read() { scanf("%d %d %d", &p, &c, &l); }
} a[MAXN];
void work() {
  for (int i = 1; i <= n; i++) a[i].read();
  int s = 0, t = n + 1;
  for (int i = 1; i <= n; i++) {
    int tot = 0, one = 0, p = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j].l <= i) {
        if (a[j].c == 1) {
          if (a[j].p > p) {
            one = j, p = a[j].p;
          }
        } else {
          tot += a[j].p;
        }
      }
    }
    tot += p;
    init();
    for (int j = 1; j <= n; j++) {
      if (a[j].l <= i && (a[j].c > 1 || j == one)) {
        if (a[j].c & 1)
          add(s, j, a[j].p);
        else
          add(j, t, a[j].p);
      }
    }
    for (int j = 1; j <= n; j++) {
      if (a[j].l <= i && (a[j].c % 2 || j == one)) {
        for (int k = 1; k <= n; k++) {
          if (a[k].l <= i && a[k].c % 2 == 0) {
            if (isprime[a[j].c + a[k].c]) {
              add(j, k, inf);
            }
          }
        }
      }
    }
    int flow = dinic(s, t, n + 2);
    if (tot - flow >= k) {
      printf("%d\n", i);
      return;
    }
  }
  puts("-1");
}
int main() {
  for (int i = 2; i <= 200000; i++) isprime[i] = true;
  for (int i = 2; i <= 200000; i++) {
    if (isprime[i]) {
      for (int j = i + i; j <= 200000; j += i) {
        isprime[j] = false;
      }
    }
  }
  while (~scanf("%d %d", &n, &k)) {
    work();
  }
  return 0;
}
