#include <bits/stdc++.h>
using namespace std;
struct SNode {
  int h, m;
};
int n, x;
SNode a1[2000], a2[2000];
int ass1[2000], ass2[2000];
inline bool cmp(const SNode &x, const SNode &y) { return x.h < y.h; }
inline void insert(int a[], int &l, const int &k) {
  int i = l;
  while (i > 0 && k < a[i - 1]) {
    a[i] = a[i - 1];
    --i;
  }
  a[i] = k;
  ++l;
}
int main(void) {
  cin >> n >> x;
  int n1 = 0, n2 = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    if (t == 0) {
      cin >> a1[n1].h >> a1[n1].m;
      ++n1;
    } else {
      cin >> a2[n2].h >> a2[n2].m;
      ++n2;
    }
  }
  sort(a1, a1 + n1, cmp);
  sort(a2, a2 + n2, cmp);
  int ans[2] = {0, 0};
  for (int w = 0; w < 2; ++w) {
    int nowx = x;
    int l1 = 0, l2 = 0;
    int al1 = 0, al2 = 0;
    for (int i = 0; i < n; ++i) {
      int p = (i & 1) ^ w;
      if (p == 0) {
        while (l1 < n1 && a1[l1].h <= nowx) {
          insert(ass1, al1, a1[l1].m);
          ++l1;
        }
        if (al1 == 0) {
          break;
        }
        nowx += ass1[--al1];
        ++ans[w];
      } else {
        while (l2 < n2 && a2[l2].h <= nowx) {
          insert(ass2, al2, a2[l2].m);
          ++l2;
        }
        if (al2 == 0) {
          break;
        }
        nowx += ass2[--al2];
        ++ans[w];
      }
    }
  }
  cout << max(ans[0], ans[1]) << endl;
  return 0;
}
