#include <bits/stdc++.h>   using namespace std;   int ar[5000009];   long long j;   int finder(vector<pair<long long, long long> > v, long long jump) {     for (int i = 0; i < v.size(); i++) {       if (v[i].first > jump) {         return i;       }     }     return v.size();   }   long long count = 0;   vector<pair<long long, long long> > v[4];   void dfs1(long long b, long long p, long long jump) {     if (v[b].size() == 0) {       ::count = max(::count, p);       return;     } else {       long long low = finder(v[b], jump);       if (v[b][0].first > jump) {         ::count = max(::count, p);         return;       } else {         low--;         long long u = 0, maxi = v[b][0].second;         for (int k = 1; k <= low; k++) {           if (maxi < v[b][k].second) {             maxi = v[b][k].second;             u = k;           }         }         jump += v[b][u].second;         v[b].erase(v[b].begin() + u);         dfs1((b == 1) ? 0 : ((b == 0) ? 1 : ((b == 2) ? 3 : 2)), p + 1, jump);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long n, jump, a, s, d;     cin >> n >> jump;     for (int i = 0; i < n; ++i) {       cin >> a >> s >> d;       if (a == 0) {         v[0].push_back(make_pair(s, d));         v[2].push_back(make_pair(s, d));       } else {         v[3].push_back(make_pair(s, d));         v[1].push_back(make_pair(s, d));       }     }     sort(v[0].begin(), v[0].end());     sort(v[1].begin(), v[1].end());     sort(v[2].begin(), v[2].end());     sort(v[3].begin(), v[3].end());     dfs1(0, 0, jump);     dfs1(3, 0, jump);     cout << ::count;     return 0;   }   