#include <bits/stdc++.h>
using namespace std;
int n, reach, type, h, mass;
int main() {
  cin >> n >> reach;
  vector<pair<int, int> > tab2, tab1, ntab2, ntab1;
  for (int i = 0; i < n; i++) {
    cin >> type >> h >> mass;
    pair<int, int> foo;
    foo = make_pair(mass, h);
    if (type == 0) {
      tab2.push_back(foo);
      ntab2.push_back(foo);
    } else {
      tab1.push_back(foo);
      ntab1.push_back(foo);
    }
  }
  sort(tab2.begin(), tab2.end());
  sort(tab1.begin(), tab1.end());
  sort(ntab2.begin(), ntab2.end());
  sort(ntab1.begin(), ntab1.end());
  int num = 0, bad = 1, nreach = reach;
  while (1) {
    bad = 1;
    for (int k = tab2.size() - 1; k >= 0; k--) {
      if (tab2[k].second <= reach && tab2[k].second != -1) {
        reach += tab2[k].first;
        num++;
        tab2[k].second = -1;
        bad = 0;
        break;
      }
    }
    if (bad) {
      break;
    }
    bad = 1;
    for (int k = tab1.size() - 1; k >= 0; k--) {
      if (tab1[k].second <= reach && tab1[k].second != -1) {
        reach += tab1[k].first;
        num++;
        tab1[k].second = -1;
        bad = 0;
        break;
      }
    }
    if (bad) {
      break;
    }
  }
  int num2 = 0;
  while (1) {
    bad = 1;
    for (int k = ntab1.size() - 1; k >= 0; k--) {
      if (ntab1[k].second <= nreach && ntab1[k].second != -1) {
        nreach += ntab1[k].first;
        num2++;
        ntab1[k].second = -1;
        bad = 0;
        break;
      }
    }
    if (bad) {
      break;
    }
    bad = 1;
    for (int k = ntab2.size() - 1; k >= 0; k--) {
      if (ntab2[k].second <= nreach && ntab2[k].second != -1) {
        nreach += ntab2[k].first;
        num2++;
        ntab2[k].second = -1;
        bad = 0;
        break;
      }
    }
    if (bad) {
      break;
    }
  }
  cout << max(num, num2);
}
