#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265359;
struct candy {
  int h, m;
};
bool cmpH(const candy &i, const candy &j) {
  return i.h < j.h || (i.h == j.h && i.m > j.m);
}
int n, x;
vector<candy> c[2];
int f(int start) {
  int ans = 0;
  priority_queue<int> pq[2];
  int ind[2] = {0, 0};
  int cur = start;
  int he = x;
  while (true) {
    for (int &i = ind[cur]; i < c[cur].size(); ++i) {
      if (c[cur][i].h <= he)
        pq[cur].push(c[cur][i].m);
      else
        break;
    }
    if (!pq[cur].empty()) {
      he += pq[cur].top();
      pq[cur].pop();
    } else
      break;
    ++ans;
    cur = 1 - cur;
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &x);
  int t;
  for (int i = 0; i < n; ++i) {
    candy cn;
    scanf("%d", &t);
    scanf("%d %d", &cn.h, &cn.m);
    c[t].push_back(cn);
  }
  for (int i = 0; i < 2; ++i) sort(c[i].begin(), c[i].end(), cmpH);
  cout << max(f(0), f(1));
  return 0;
}
