#include <bits/stdc++.h>
using namespace std;
int process(list<pair<int, int>> t0, list<pair<int, int>> t1, int x) {
  auto result = 0;
  while (!t0.empty() && !t1.empty()) {
    auto max = 0;
    auto max_it = t0.end();
    for (auto it = t0.begin(); it != t0.end(); it++) {
      if ((*it).first <= x && (*it).second > max) {
        max_it = it;
        max = (*it).second;
      }
    }
    if (max_it != t0.end()) {
      x += max;
      t0.erase(max_it);
      swap(t0, t1);
      result++;
    } else {
      break;
    }
  }
  if (!t0.empty()) {
    for (auto& i : t0) {
      if (i.first <= x) {
        result++;
        break;
      }
    }
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, x;
  cin >> n >> x;
  list<pair<int, int>> t0, t1;
  for (int t, h, m; cin >> t >> h >> m;) {
    if (t == 1) {
      t1.emplace_front(h, m);
    } else {
      t0.emplace_front(h, m);
    }
  }
  cout << max(process(t0, t1, x), process(t1, t0, x));
  return 0;
}
