#include <bits/stdc++.h>
using namespace std;
struct list {
  int h, y;
  friend bool operator<(const list &a, const list &b) { return a.h < b.h; }
} node[4000], ps[4000];
priority_queue<int> sta;
priority_queue<int> stb;
int main() {
  int n, x, y, z, xx;
  while (~scanf("%d%d", &n, &x)) {
    int a, b;
    a = b = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &xx, &y, &z);
      if (xx == 0) {
        node[a].h = y;
        node[a++].y = z;
      } else {
        ps[b].h = y;
        ps[b++].y = z;
      }
    }
    sort(node, node + a);
    sort(ps, ps + b);
    int p, pp;
    p = pp = 0;
    int nn = x;
    int i, j, t;
    i = j = 0;
    nn = x;
    while (!sta.empty()) sta.pop();
    while (!stb.empty()) stb.pop();
    while (1) {
      for (; i < a; i++) {
        if (node[i].h <= nn)
          sta.push(node[i].y);
        else
          break;
      }
      if (sta.empty()) break;
      t = sta.top();
      sta.pop();
      p++;
      nn += t;
      for (; j < b; j++) {
        if (ps[j].h <= nn)
          stb.push(ps[j].y);
        else
          break;
      }
      if (stb.empty()) break;
      t = stb.top();
      stb.pop();
      p++;
      nn += t;
    }
    i = j = 0;
    nn = x;
    while (!sta.empty()) sta.pop();
    while (!stb.empty()) stb.pop();
    while (1) {
      for (; j < b; j++) {
        if (ps[j].h <= nn)
          stb.push(ps[j].y);
        else
          break;
      }
      if (stb.empty()) break;
      t = stb.top();
      stb.pop();
      pp++;
      nn += t;
      for (; i < a; i++) {
        if (node[i].h <= nn)
          sta.push(node[i].y);
        else
          break;
      }
      if (sta.empty()) break;
      t = sta.top();
      sta.pop();
      pp++;
      nn += t;
    }
    cout << max(p, pp) << endl;
  }
  return 0;
}
