#include <bits/stdc++.h>
using namespace std;
int N, X;
vector<pair<int, int> > C[2];
int solve(vector<pair<int, int> >& A, vector<pair<int, int> >& B) {
  int ret = 0, x = X;
  multiset<int> sA, sB;
  for (int i = 0, a = 0, b = 0; i < N; i++)
    if (!(i & 1)) {
      for (; a < int(A.size()) && x >= A[a].first; a++) {
        sA.insert(-A[a].second);
      }
      if (sA.empty()) break;
      x -= *sA.begin();
      ret++;
      sA.erase(sA.begin());
    } else {
      for (; b < int(B.size()) && x >= B[b].first; b++) {
        sB.insert(-B[b].second);
      }
      if (sB.empty()) break;
      x -= *sB.begin();
      ret++;
      sB.erase(sB.begin());
    }
  return ret;
}
int main() {
  cin >> N >> X;
  for (int i = 0; i < N; i++) {
    int t, h, m;
    cin >> t >> h >> m;
    C[t].push_back(pair<int, int>(h, m));
  }
  sort(C[0].begin(), C[0].end());
  sort(C[1].begin(), C[1].end());
  int ans = max(solve(C[0], C[1]), solve(C[1], C[0]));
  cout << ans << endl;
  return 0;
}
