#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > candies;
int eat(int type, int x) {
  int n = candies.size(), res = 0;
  vector<int> used(n);
  while (true) {
    int best = -1;
    for (int i = 0; i < n; ++i) {
      if (!used[i] && candies[i].second.second == type &&
          x >= candies[i].first) {
        if (best == -1 ||
            candies[i].second.first > candies[best].second.first) {
          best = i;
        }
      }
    }
    if (best == -1) {
      break;
    } else {
      used[best] = 1;
      x += candies[best].second.first;
      ++res;
      type = 1 - type;
    }
  }
  return res;
}
int main() {
  int n, x;
  cin >> n >> x;
  for (int i = 0; i < n; ++i) {
    int t, h, m;
    cin >> t >> h >> m;
    candies.push_back(make_pair(h, make_pair(m, t)));
  }
  cout << max(eat(0, x), eat(1, x)) << endl;
  return 0;
}
