#include <bits/stdc++.h>
using namespace std;
const int maxN = 2000 + 10;
struct node {
  int h, w, t;
  inline bool operator<(const node &s) const { return w > s.w; }
  inline void in() { scanf("%d %d", &h, &w); }
};
node a[maxN], b[maxN];
int na, nb;
int gao(int cur, int tot) {
  for (int i = 0; i < na; i++) a[i].t = -1;
  for (int i = 0; i < nb; i++) b[i].t = -1;
  int res = 0, chk;
  while (1) {
    if (cur == 0) {
      chk = 0;
      for (int i = 0; i < na; i++)
        if (a[i].t == -1 && a[i].h <= tot) {
          a[i].t = 1;
          chk = 1;
          tot += a[i].w;
          res++;
          cur = 1;
          break;
        }
      if (chk)
        continue;
      else
        break;
    }
    if (cur == 1) {
      chk = 0;
      for (int i = 0; i < nb; i++)
        if (b[i].t == -1 && b[i].h <= tot) {
          b[i].t = 1;
          chk = 1;
          tot += b[i].w;
          res++;
          cur = 0;
          break;
        }
      if (chk)
        continue;
      else
        break;
    }
  }
  return res;
}
int n, x;
int main() {
  cin >> n >> x;
  na = nb = 0;
  int t;
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (t == 0)
      a[na++].in();
    else
      b[nb++].in();
  }
  sort(a, a + na);
  sort(b, b + nb);
  int res = max(gao(0, x), gao(1, x));
  cout << res << endl;
  return 0;
}
