#include <bits/stdc++.h>
using namespace std;
int re, n, x, y;
vector<pair<int, int> > a, b;
priority_queue<int> pqa, pqb;
void input() {
  scanf("%d %d", &n, &x);
  for (int i = 0; i < n; i++) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    if (!t) {
      a.push_back(pair<int, int>(h, m));
    } else {
      b.push_back(pair<int, int>(h, m));
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
}
int get(int cur) {
  int aidx, bidx;
  aidx = bidx = 0;
  int cnt = 0;
  int cury = x;
  while (1) {
    if (aidx > a.size() && bidx > b.size()) break;
    if (cur == 0) {
      for (int i = aidx; i < a.size(); i++) {
        if (cury >= a[i].first) {
          pqa.push(a[i].second);
          aidx++;
        } else
          break;
      }
      if (pqa.empty()) break;
      cury += pqa.top();
      pqa.pop();
      cur = 1;
      cnt++;
    } else {
      for (int i = bidx; i < b.size(); i++) {
        if (cury >= b[i].first) {
          pqb.push(b[i].second);
          bidx++;
        } else
          break;
      }
      if (pqb.empty()) break;
      cury += pqb.top();
      pqb.pop();
      cur = 0;
      cnt++;
    }
  }
  while (!pqa.empty()) pqa.pop();
  while (!pqb.empty()) pqb.pop();
  return cnt;
}
void solve() {
  input();
  re = max(get(0), get(1));
  printf("%d\n", re);
}
int main(void) {
  solve();
  return 0;
}
