#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a[2];
int n, x;
int main(int argc, char *argv[]) {
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    int t, h, m;
    cin >> t >> h >> m;
    a[t].push_back(make_pair(h, m));
  }
  int ans = 0;
  for (int start = 0; start <= 1; start++) {
    int xx = x, next = start;
    int res = 0;
    while (true) {
      int idx = -1;
      for (int i = 0; i < a[next].size(); i++)
        if (a[next][i].first <= xx && a[next][i].first > 0)
          if (idx < 0 || a[next][i].second > a[next][idx].second) idx = i;
      if (idx < 0) break;
      a[next][idx].first = -a[next][idx].first;
      xx += a[next][idx].second;
      res++;
      next ^= 1;
    }
    for (int i = 0; i < 2; i++)
      for (typeof((a[i]).begin()) it = (a[i]).begin(); it != (a[i]).end(); it++)
        it->first = abs(it->first);
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}
