#include <bits/stdc++.h>
using namespace std;
struct node {
  int va;
  int h;
};
node a[2005];
node b[2005];
bool visa[2005];
bool visb[2005];
int main() {
  int n, m;
  cin >> n >> m;
  int cnt1 = 0;
  int cnt2 = 0;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      a[cnt1].h = y;
      a[cnt1].va = z;
      cnt1++;
    } else {
      b[cnt2].h = y;
      b[cnt2].va = z;
      cnt2++;
    }
  }
  int ans1 = 0;
  int ans2 = 0;
  int res = 0;
  int M = m;
  while (1) {
    int mx = -1;
    int st = -1;
    if (res % 2) {
      for (int i = 0; i < cnt1; i++) {
        if (visa[i]) continue;
        if (a[i].h <= m) {
          st = i;
          mx = max(mx, a[i].va);
        }
      }
      if (st == -1) break;
      m += mx;
      visa[st] = 1;
    } else {
      for (int i = 0; i < cnt2; i++) {
        if (visb[i]) continue;
        if (b[i].h <= m) {
          st = i;
          mx = max(mx, b[i].va);
        }
      }
      if (st == -1) break;
      m += mx;
      visb[st] = 1;
    }
    if (st == -1) break;
    res++;
  }
  ans1 = res;
  res = 0;
  memset(visa, 0, sizeof(visa));
  memset(visb, 0, sizeof(visb));
  m = M;
  while (1) {
    int mx = -1;
    int st = -1;
    if (res % 2 == 0) {
      for (int i = 0; i < cnt1; i++) {
        if (visa[i]) continue;
        if (a[i].h <= m) {
          st = i;
          mx = max(mx, a[i].va);
        }
      }
      if (st == -1) break;
      m += mx;
      visa[st] = 1;
    } else {
      for (int i = 0; i < cnt2; i++) {
        if (visb[i]) continue;
        if (b[i].h <= m) {
          st = i;
          mx = max(b[i].va, mx);
        }
      }
      if (st == -1) break;
      m += mx;
      visb[st] = 1;
    }
    if (st == -1) break;
    res++;
  }
  ans2 = res;
  ans2 = res;
  cout << max(ans1, ans2) << endl;
}
