#include <bits/stdc++.h>
using namespace std;
class node {
 public:
  int m;
  int h;
};
bool compare(node a, node b) { return a.h < b.h; }
int find(int x, std::vector<node> &v1, vector<node> &v2) {
  std::vector<int> heap1;
  std::vector<int> heap2;
  heap1.push_back(0);
  heap2.push_back(0);
  make_heap(heap1.begin(), heap1.end());
  make_heap(heap2.begin(), heap2.end());
  int i = 0;
  while (i < v1.size() && v1[i].h <= x) {
    heap1.push_back(v1[i].m);
    push_heap(heap1.begin(), heap1.end());
    i++;
  }
  int j = 0;
  int count = 0, temp;
  while (heap1.size() > 1) {
    x += heap1.front();
    pop_heap(heap1.begin(), heap1.end());
    heap1.pop_back();
    count++;
    while (j < v2.size() && v2[j].h <= x) {
      heap2.push_back(v2[j].m);
      push_heap(heap2.begin(), heap2.end());
      j++;
    }
    if (heap2.size() < 2) {
      break;
    }
    x += heap2.front();
    pop_heap(heap2.begin(), heap2.end());
    heap2.pop_back();
    count++;
    while (i < v1.size() && v1[i].h <= x) {
      heap1.push_back(v1[i].m);
      push_heap(heap1.begin(), heap1.end());
      i++;
    }
  }
  return count;
}
int main() {
  int n, x;
  cin >> n >> x;
  vector<node> v1;
  vector<node> v2;
  node temp;
  bool flag;
  for (int i = 0; i < n; ++i) {
    cin >> flag >> temp.h >> temp.m;
    if (flag) {
      v1.push_back(temp);
    } else
      v2.push_back(temp);
  }
  sort(v1.begin(), v1.end(), compare);
  sort(v2.begin(), v2.end(), compare);
  int count1, count2;
  count1 = find(x, v1, v2);
  count2 = find(x, v2, v1);
  if (count1 < count2) {
    count1 = count2;
  }
  cout << count1;
}
