#include <bits/stdc++.h>
using namespace std;
struct Sweet {
  int h, m;
};
int n, x, xt;
int t, h, m;
Sweet ss;
vector<Sweet> v0, v1, vt0, vt1;
int pos, ans1 = 0, ans2 = 0, ans, flag;
bool cmp(Sweet x, Sweet y) { return x.h < y.h; }
int findneed() {
  int maxv = -1, maxi = -1;
  if (flag == 0) {
    for (int k = 0; k < vt0.size() && xt >= vt0[k].h; ++k) {
      if (vt0[k].m > maxv) {
        maxv = vt0[k].m;
        maxi = k;
      }
    }
  } else {
    for (int k = 0; k < vt1.size() && xt >= vt1[k].h; ++k) {
      if (vt1[k].m > maxv) {
        maxv = vt1[k].m;
        maxi = k;
      }
    }
  }
  return maxi;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> x;
  for (int i = 0; i < n; ++i) {
    cin >> t >> h >> m;
    ss.h = h;
    ss.m = m;
    if (t == 0)
      v0.push_back(ss);
    else
      v1.push_back(ss);
  }
  sort(v0.begin(), v0.end(), cmp);
  sort(v1.begin(), v1.end(), cmp);
  vt0 = v0;
  vt1 = v1;
  xt = x;
  flag = 0;
  while ((pos = findneed()) != -1) {
    if (flag == 0) {
      xt += vt0[pos].m;
      vt0.erase(vt0.begin() + pos);
    } else {
      xt += vt1[pos].m;
      vt1.erase(vt1.begin() + pos);
    }
    ans1++;
    flag = 1 - flag;
  }
  vt0 = v0;
  vt1 = v1;
  xt = x;
  flag = 1;
  while ((pos = findneed()) != -1) {
    if (flag == 0) {
      xt += vt0[pos].m;
      vt0.erase(vt0.begin() + pos);
    } else {
      xt += vt1[pos].m;
      vt1.erase(vt1.begin() + pos);
    }
    ans2++;
    flag = 1 - flag;
  }
  cout << max(ans1, ans2) << endl;
  return 0;
}
