#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > f, c;
int main() {
  int n, h;
  cin >> n >> h;
  int origh = h;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (!t) {
      int curh, curm;
      cin >> curh >> curm;
      c.push_back(make_pair(curh, curm));
    } else {
      int curh, curm;
      cin >> curh >> curm;
      f.push_back(make_pair(curh, curm));
    }
  }
  sort(c.rbegin(), c.rend());
  sort(f.rbegin(), f.rend());
  vector<pair<int, int> > origc = c;
  vector<pair<int, int> > origf = f;
  priority_queue<int, vector<int> > cpq;
  while (c.size() && c[c.size() - 1].first <= h) {
    cpq.push(c[c.size() - 1].second);
    c.pop_back();
  }
  priority_queue<int, vector<int> > fpq;
  while (f.size() && f[f.size() - 1].first <= h) {
    fpq.push(f[f.size() - 1].second);
    f.pop_back();
  }
  int count1 = 0;
  bool curturn = 0;
  while (1) {
    if (!curturn) {
      if (!cpq.size()) {
        break;
      }
      h += cpq.top();
      cpq.pop();
      while (f.size() && f[f.size() - 1].first <= h) {
        fpq.push(f[f.size() - 1].second);
        f.pop_back();
      }
    } else {
      if (!fpq.size()) {
        break;
      }
      h += fpq.top();
      fpq.pop();
      while (c.size() && c[c.size() - 1].first <= h) {
        cpq.push(c[c.size() - 1].second);
        c.pop_back();
      }
    }
    curturn = !curturn;
    count1++;
  }
  while (!fpq.empty()) {
    fpq.pop();
  }
  while (!cpq.empty()) {
    cpq.pop();
  }
  c = origc;
  f = origf;
  h = origh;
  int count2 = 0;
  while (c.size() && c[c.size() - 1].first <= h) {
    cpq.push(c[c.size() - 1].second);
    c.pop_back();
  }
  while (f.size() && f[f.size() - 1].first <= h) {
    fpq.push(f[f.size() - 1].second);
    f.pop_back();
  }
  curturn = 1;
  while (1) {
    if (!curturn) {
      if (!cpq.size()) {
        break;
      }
      h += cpq.top();
      cpq.pop();
      while (f.size() && f[f.size() - 1].first <= h) {
        fpq.push(f[f.size() - 1].second);
        f.pop_back();
      }
    } else {
      if (!fpq.size()) {
        break;
      }
      h += fpq.top();
      fpq.pop();
      while (c.size() && c[c.size() - 1].first <= h) {
        cpq.push(c[c.size() - 1].second);
        c.pop_back();
      }
    }
    curturn = !curturn;
    count2++;
  }
  cout << max(count1, count2) << endl;
  return 0;
}
