#include <bits/stdc++.h>
#pragma GCC optimize(2)
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const int N = 3e3 + 7;
int n, hi, fl = 0, ans = 0;
struct node {
  int t, h, m;
};
bool cmp(node l, node r) {
  if (l.m == r.m) return l.h < r.h;
  return l.m > r.m;
}
vector<node> v, vv;
int main() {
  scanf("%d%d", &n, &hi);
  for (int i = 1; i <= n; i++) {
    int t, h, mm;
    scanf("%d%d%d", &t, &h, &mm);
    v.push_back({t, h, mm});
    vv.push_back({t, h, mm});
  }
  sort(v.begin(), v.end(), cmp);
  sort(vv.begin(), vv.end(), cmp);
  int high = hi, cnt = 0;
  fl = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].t == fl && v[i].h <= high) {
      high += v[i].m;
      cnt++;
      fl ^= 1;
      v.erase(v.begin() + i);
      i = -1;
    }
  }
  ans = max(ans, cnt);
  cnt = 0, high = hi, fl = 1;
  for (int i = 0; i < vv.size(); i++) {
    if (vv[i].t == fl && vv[i].h <= high) {
      high += vv[i].m;
      cnt++;
      fl ^= 1;
      vv.erase(vv.begin() + i);
      i = -1;
    }
  }
  ans = max(ans, cnt);
  printf("%d\n", ans);
  return 0;
}
