#include <bits/stdc++.h>
using namespace std;
int t[5000], h[5000], c[8000], book[5000];
int main() {
  int n, m, ans = 0, ans1 = 0;
  scanf("%d%d", &n, &m);
  int tmp2 = m;
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &t[i], &h[i], &c[i]);
  int flag = 1;
  while (1) {
    int maxn = -1;
    for (int i = 1; i <= n; i++)
      if (!book[i] && t[i] == flag && h[i] <= m &&
          (maxn == -1 || c[maxn] < c[i]))
        maxn = i;
    if (maxn == -1) break;
    book[maxn]++;
    ans++;
    flag = !flag;
    m += c[maxn];
  }
  flag = 0;
  memset(book, 0, sizeof(book));
  m = tmp2;
  while (1) {
    int maxn = -1;
    for (int i = 1; i <= n; i++)
      if (!book[i] && t[i] == flag && h[i] <= m &&
          (maxn == -1 || c[maxn] < c[i]))
        maxn = i;
    if (maxn == -1) break;
    book[maxn]++;
    ans1++;
    flag = !flag;
    m += c[maxn];
  }
  printf("%d", max(ans, ans1));
  return 0;
}
