//#include<cstdio>
//#include<cstdlib>
#include <iostream>
#include <vector>
#include <queue>
//#include<bitset>
//#include<string>
//#include<stack>
//#include<set>
//#include<unordered_set>
//#include<map>
//#include<unordered_map>
//#include<cstring>
//#include<complex>
#include <math.h>
//#include<iomanip>
//#include<numeric>
#include <algorithm>
#include <stdio.h>
//#include<list>
//#include<functional>
//#include<cassert>
using namespace std;
#define ll long long int
#define llu unsigned long long int
//#define v vector<ll>
//#define vv vector<ll>
void pv(vector<ll> arr, ll size)
{

    for (ll i = 0; i < size; i++)
    {
        cout << arr[i] << ' ';
    }
}
void gv(vector<ll> &arr)
{
    ll size = arr.size();
    for (ll i = 0; i < size; i++)
    {
        cin >> arr[i];
    }
}
ll tim = 0;
void dfs(vector<vector<ll>> &adj, ll n, ll vert, vector<ll> &vis, vector<pair<ll, ll>> &a, vector<ll> &level, ll ht)
{
    vis[vert] = 1;
    level[vert] = ht;
    a[vert].first = tim++;
    for (ll i = 0; i < adj[vert].size(); i++)
    {
        if (vis[adj[vert][i]] == 0)
            dfs(adj, n, adj[vert][i], vis, a, level, ht + 1);
    }
    a[vert].second = tim++;
}
class segTree
{
    ll identity, size;
    vector<ll> segtree, arr;
    ll f(ll x, ll y)
    {
        // put function here
        return x + y;
    }
    void buildseg()
    {
        ll n = size;
        for (ll i = n - 1; i > 0; --i)
            segtree[i] = f(segtree[i << 1], segtree[i << 1 | 1]);
    }
    void update(ll p, ll value)
    {
        ll n = size;
        // r++;
        for (segtree[p += n] = value; p > 1; p >>= 1)
            segtree[p >> 1] = f(segtree[p], segtree[p ^ 1]);
    }
    ll query(ll l, ll r)
    {
        ll resl = identity, resr = identity, n = size;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1)
        {
            if (l & 1)
                resl = f(resl, segtree[l++]);
            if (r & 1)
                resr = f(segtree[--r], resr);
        }
        return f(resl, resr);
    }
    void buildsegrec(ll start, ll end, ll ind)
    {
        if (start == end)
            segtree[ind] = arr[start];
        else
        {
            ll mid = (end - start) / 2 + start;
            buildsegrec(start, mid, 2 * ind + 1);
            buildsegrec(mid + 1, end, 2 * ind + 2);
            segtree[ind] = f(segtree[2 * ind + 1], segtree[2 * ind + 2]);
        }
    }
    void updaterec(ll ind, ll i_to_update, ll val, ll start, ll end)
    {
        if (start == end)
        {
            arr[i_to_update] += val;
            segtree[ind] += val;
            return;
        }
        ll mid = (end - start) / 2 + start;
        if (i_to_update > mid)
        {
            updaterec(2 * ind + 2, i_to_update, val, mid + 1, end);
        }
        else
        {
            updaterec(2 * ind + 1, i_to_update, val, start, mid);
        }
        segtree[ind] = f(segtree[2 * ind + 1], segtree[2 * ind + 2]);
    }
    ll queryrec(ll ind, ll start, ll end, ll left, ll right)
    {
        if (right < start || left > end)
            return identity;
        if (left <= start && right >= end)
        {
            return segtree[ind];
        }
        ll mid = (end - start) / 2 + start;
        ll q1 = queryrec(2 * ind + 1, start, mid, left, right);
        ll q2 = queryrec(2 * ind + 2, mid + 1, end, left, right);
        return f(q1, q2);
    }

  public:
    segTree(ll n, vector<ll> &ar, ll ident, ll def)
    {
        segtree = vector<ll>(4 * n, def);
        arr = ar;
        identity = ident;
        buildsegrec(0, n - 1, 0);
        // buildseg();
        size = n;
    }
    void updateTree(ll i_to_update, ll val)
    {
        updaterec(0, i_to_update, val, 0, size - 1);
        // update(i_to_update, val);
    }
    ll querySeg(ll l, ll r)
    {
        return queryrec(0, 0, size - 1, l, r);
        // return query(l, r);
    }
    void printSeg()
    {
        pv(segtree, 4 * size);
    }
};
int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n, m;
    cin >> n >> m;
    vector<ll> arr(n);
    vector<vector<ll>> adj(n + 1);
    gv(arr);
    for (ll i = 0; i < n - 1; i++)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    vector<pair<ll, ll>> a(n + 1);
    vector<ll> vis(n + 1, 0);
    vector<ll> dd(2 * n, 0), level(n + 1, 0);
    vector<ll> pp(2 * n, 0);
    dfs(adj, n, 1, vis, a, level, 0);
    // for (ll i = 1; i <= n; i++)
    // {
    //     cout << a[i].first << " " << a[i].second << " " << level[i] << '\n';
    // }
    segTree s(2 * n, dd, 0, 0);
    // s.printSeg();
    for (ll i = 0; i < m; i++)
    {
        ll x, y, z;
        cin >> x;
        if (x == 1)
        {
            cin >> y >> z;
            if (level[y] % 2 == 1)
            {
                s.updateTree(a[y].first, +z);
                if (a[y].second + 1 < 2 * n)
                    s.updateTree(a[y].second + 1, -z);
                // dd[y - 1] += z;
                // pp[y - 1] -= z;
            }
            else
            {
                s.updateTree(a[y].first, -z);
                if (a[y].second + 1 < 2 * n)
                    s.updateTree(a[y].second + 1, z);
                // dd[y - 1] -= z;
                // pp[y - 1] += z;
            }
        }
        else
        {
            cin >> y;
            ll sum = s.querySeg(0, a[y].first);
            if (level[y] % 2 == 1)
                cout << arr[y - 1] + sum << '\n';
            else
                cout << arr[y - 1] - sum << '\n';
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ri(x) scanf("%d",&(x))
#define ri2(x,y) scanf("%d %d",&(x),&(y))
#define ri3(x,y,z) scanf("%d %d %d",&(x),&(y),&(z))
#define rll(x) scanf("%lld",&(x))
#define rll2(x,y) scanf("%lld %lld",&(x),&(y))
#define rll3(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z))
#define gc(x) ((x) = getchar())
using namespace::std;

const long double PI = acos(-1);
const long long MOD = 1000000000 +7;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> tll;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<tll> vtll;
typedef vector<string> vs;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;

ll gcd(ll a, ll b){ return b==0?a:gcd(b,a%b);}

ll add(ll a, ll b, ll m = MOD){ return (a%m + b%m+2*m)%m;}

ll mul(ll a, ll b, ll m = MOD){ return ((a%m+m)*(b%m+m))%m;}

ll pow_mod(ll a, ll b, ll m = MOD){
	ll res = 1LL;
	a = a%m;
	while(b){
		if(b&1) res = mul(res,a,m);
		b >>= 1;
		a = mul(a,a,m);
	}
	return res;
}

ll fastexp(ll a, ll b){
	ll res = 1LL;
	while(b){
		if(b&1) res = res*a;
		b >>= 1;
		a *= a;
	}
	return res;
}

int gcdExtendido(int a, int b, int *x, int *y){
	if(a == 0){
		*x = 0;
		*y = 1;
		return b;
	}
	int x1, y1;
	int gcd = gcdExtendido(b%a,a,&x1,&y1);

	*x = y1-(b/a)*x1;
	*y = x1;
	return gcd;
}

int modInverso(int a, int m){
	int x, y;
	int g = gcdExtendido(a,m,&x,&y);
	if(g!=1) return -1;
	else return (x%m + m)%m;
}

/****************************************
*************P*L*A*N*T*I*L*L*A************
*****************************************/

class FenwickTree {
	vi f;
	int n;
	public:
	FenwickTree(){};
	FenwickTree(int n){
		this->n = n;
		f.assign(n+1,0);
	}

	int getSum(int i){
		int ans = 0;
		i++;
		while(i > 0){
			ans += f[i];
			i &= i-1;
		}
		return ans;
	}

	void update(int i, int val){
		i++;
		while(i <= n){
			f[i] += val;
			i += (i&(-i));
		}
	}

	int query(int L, int R){
		return getSum(R) - getSum(L-1);
	}

};

const int N = 200000+5;

int n, m;
int c[N];
int L[N];
int R[N];
int T;
vi H[N];
vi G[N];
int bsize;
int level[N];
int parent[N];
int pos_in_level[N];
int max_level;
FenwickTree F[N];

void DFS(int u, int p = 0){
	parent[u] = p;
	level[u] = level[p] + 1;
	max_level = max(max_level,level[u]);
	H[level[u]].pb(u);
	L[u] = T++;
	for(int i=0; i<G[u].size(); i++){
		int v = G[u][i];
		if(v != p){
			DFS(v,u);
		}
	}
	R[u] = T-1;
}

void preprocess(){
	DFS(1);
	bsize = max(int(sqrt(n)),2);
	for(int i=1; i<=max_level; i++){
		F[i] = FenwickTree(H[i].size()+4);
		for(int j=0; j<H[i].size(); j++){
			pos_in_level[H[i][j]] = j;
		}
	}
}

void update(int u, int val){
	for(int i=level[u]; i<=max_level; i+=bsize){
		int lo = 0, hi = H[i].size()-1;
		while(lo < hi){
			int mi = lo + (hi-lo)/2;
			if(L[H[i][mi]] < L[u]) lo = mi+1;
			else hi = mi;
		}
		if(L[u] <= L[H[i][lo]]){
			int lower = lo;
			hi = H[i].size()-1;
			while(lo < hi){
				int mi = lo + (hi-lo+1)/2;
				if(L[H[i][mi]] <= R[u]) lo = mi;
				else hi = mi-1;
			}
			int val2 = val;
			if((i - level[u])&1) val2 = -val;
			F[i].update(lower,val2);
			F[i].update(lo+1,-val2);
		}
	}
}

int query(int u){
	int ans = 0;
	int sign = 1;
	for(int i=0; i<bsize and level[u]; i++){
		ans += sign*F[level[u]].query(0,pos_in_level[u]);
		u = parent[u];
		sign = -sign;
	}
	return ans;
}

int main(){
	ri2(n,m);
	for(int i=1; i<=n; i++){
		ri(c[i]);
	}
	int a,b;
	for(int i=1; i<n; i++){
		ri2(a,b);
		G[a].pb(b);
		G[b].pb(a);
	}
	preprocess();
	int op;
	while(m--){
		ri(op);
		if(op == 1){
			ri2(a,b);
			update(a,b);
		}
		else{
			ri(a);
			printf("%d\n",c[a]+query(a));
		}
	}
	return 0;
}


#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> //priority_queue<int, vector<int>, greater<int> > q;
#include <map>
#include <set> //multiset set<int,greater<int>>大到小
#include <vector>// vector<int>().swap(v);清空释放内存
#include <stack>
#include <cmath> // auto &Name : STLName  Name.
#include <utility>
#include <sstream>
#include <string>//__builtin_popcount(ans);//获取某个数二进制位1的个数
#define mod 1000000007
#define mod9 998244353
#define ld long double
#define ull unsigned long long
#define db double
#define ll long long
const db eps=1e-7;
//const db pi=acos(-1);
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
#define rep(i,be,en) for (int i=be;i<=en;i++)
#define per(i,be,en) for (int i=en;i>=be;i--)
using namespace std;
const int N=2e3+7;

int n,hi,fl=0,ans=0;
struct node{
    int t,h,m;
};
bool cmp(node l,node r){
    if(l.m==r.m) return l.h<r.h;
    return l.m>r.m;
}
vector<node> v,vv;
int main(){
    scanf("%d%d",&n,&hi);
    rep(i,1,n){
        int t,h,mm;
        scanf("%d%d%d",&t,&h,&mm);
        v.push_back({t,h,mm});
        vv.push_back({t,h,mm});
    }
    sort(v.begin(),v.end(),cmp);
    int high=hi,cnt=0;
    fl=0;
    for(int i=0;i<v.size();i++){
        if(v[i].t==fl&&v[i].h<=high){
            high+=v[i].m;
//            printf("high1=%d\n",high);
            cnt++;
            fl^=1;
            v.erase(v.begin()+i);
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].t);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].h);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].m);
//            printf("\n");
            i=-1;
        }
    }
    ans=max(ans,cnt);
    cnt=0,high=hi,fl=1;
    for(int i=0;i<vv.size();i++){
        if(vv[i].t==fl&&vv[i].h<=high){
            high+=vv[i].m;
//            printf("high2=%d\n",high);
            cnt++;
            fl^=1;
            vv.erase(vv.begin()+i);
            i=-1;
        }
    }
    ans=max(ans,cnt);
    printf("%d\n",ans);
    return 0;
}


#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> //priority_queue<int, vector<int>, greater<int> > q;
#include <map>
#include <set> //multiset set<int,greater<int>>大到小
#include <vector>// vector<int>().swap(v);清空释放内存
#include <stack>
#include <cmath> // auto &Name : STLName  Name.
#include <utility>
#include <sstream>
#include <string>//__builtin_popcount(ans);//获取某个数二进制位1的个数
#define mod 1000000007
#define mod9 998244353
#define ld long double
#define ull unsigned long long
#define db double
#define ll long long
const db eps=1e-7;
//const db pi=acos(-1);
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
#define rep(i,be,en) for (int i=be;i<=en;i++)
#define per(i,be,en) for (int i=en;i>=be;i--)
using namespace std;
const int N=2e3+7;

int n,hi,fl=0,ans=0;
struct node{
    int t,h,m;
};
bool cmp(node l,node r){
//    if(l.m==r.m) return l.h<r.h;
    return l.m>r.m;
}
vector<node> v,vv;
int main(){
    scanf("%d%d",&n,&hi);
    rep(i,1,n){
        int t,h,mm;
        scanf("%d%d%d",&t,&h,&mm);
        v.push_back({t,h,mm});
        vv.push_back({t,h,mm});
    }
    sort(v.begin(),v.end(),cmp);
    int high=hi,cnt=0;
    fl=0;
    for(int i=0;i<v.size();i++){
        if(v[i].t==fl&&v[i].h<=high){
            high+=v[i].m;
//            printf("high1=%d\n",high);
            cnt++;
            fl^=1;
            v.erase(v.begin()+i);
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].t);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].h);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].m);
//            printf("\n");
            i=-1;
        }
    }
    ans=max(ans,cnt);
    cnt=0,high=hi,fl=1;
    for(int i=0;i<vv.size();i++){
        if(vv[i].t==fl&&vv[i].h<=high){
            high+=vv[i].m;
//            printf("high2=%d\n",high);
            cnt++;
            fl^=1;
            vv.erase(vv.begin()+i);
            i=-1;
        }
    }
    ans=max(ans,cnt);
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using  namespace std;
const int N = 1e5;
int n , x , t , h , m , ans , x1;
vector<pair<pair<int , int> , int>> ve;
bool visit[N];
int solve( bool type)
{
     for(int i = 0 ; i < n ;++i)
     {
          if(!visit[i] && ve[i].second == type && ve[i].first.second <= x)
          {
               visit[i] = true;
               x += ve[i].first.first;
               return solve(!type)+1;
          }
     }
     return 0;
}
int main() {
 scanf("%d%d",&n,&x);
 for(int i = 0 ; i < n ; ++i)
 {
      scanf("%d%d%d",&t ,&h ,&m);
      ve.push_back({{m , h} , t});
 }
 sort(ve.rbegin() , ve.rend());
 x1 = x;
 int ans1 = solve(0);
 memset(visit , 0 , sizeof(visit));
 x  =  x1 ;
 int ans2 = solve(1);

 cout<<max(ans1 , ans2);

   return 0;
}


#include <bits/stdc++.h>
using namespace std ;
const int N =1e6+10;
int ans , ans1 , n  , x , x1 , t ,h , m;
bool visit[N] , visit1[N];
vector<pair<pair<int , int> , int>> ve;
int solve( bool type){
     for(int i = 0 ; i < n ; ++i )
     {
      if(!visit[i] && ve[i].second == type && ve[i].first.second <= x)
          {
               visit[i] = true;
               x += ve[i].first.first;
               ans++;
               return solve(!type)+1;
          }
     }
     return 0;
}
int main() {
  scanf("%d%d",&n,&x);
 for(int i = 0 ; i < n ; ++i)
 {
      scanf("%d%d%d",&t ,&h ,&m);
      ve.push_back({{m , h} , t});
 }
 sort(ve.rbegin() , ve.rend());
 x1 = x;
  ans = solve(0);
 memset(visit , 0 , sizeof(visit));
 x = x1;
 ans1 = solve(1);
 cout<<max(ans1 , ans1);

   return 0;
}


#include <bits/stdc++.h>
using namespace std ;
const int N =1e6+10;
int ans , ans1 , n  , x , x1;
bool visit[N] , visit1[N];
vector<pair<int, pair<int, int>>>ve;
void solve(int type){
    for(int i = 0 ; i < n ; ++i){
       if(x >= ve[i].second.first && !visit[i] && type!=ve[i].second.second){
           x+=ve[i].first ;
              visit[i] = 1 ;
           ans++;
           solve(ve[i].second.second);
       }
    }
}
void solve1(int type){
    for(int i = 0 ; i < n ; ++i){
       if(x1 >= ve[i].second.first && !visit1[i] && type!=ve[i].second.second){
           x1+=ve[i].first ;
             visit1[i] = 1 ;
           ans1++;
           solve(ve[i].second.second);
       }
    }
}
int main() {
    scanf("%d%d",&n,&x);
     for(int t , h , m , i = 0 ; i < n ; ++i){
       scanf("%d%d%d" , &t , &h , &m);
       ve.push_back({m , {h , t }});
     }
     x1 = x ;
     sort(ve.rbegin() , ve.rend());
       solve(0) ; solve1(1);
       cout<<max(ans , ans1);
}


#include <bits/stdc++.h>
using namespace std ;
const int N =1e6+10;
int ans , ans1 , n  , x , x1;
bool visit[N] , visit1[N];
vector<pair<int, pair<int, int>>>ve;
int solve( bool type){
     for(int i = 0 ; i < n ; ++i )
     {
          if(!visit[i] && ve[i].second.second == type && ve[i].second.first <= x)
          {
               visit[i] = true;
               x += ve[i].first;
               ans++;
               return solve(!type)+1;
          }
     }
     return 0;
}
int main() {
    scanf("%d%d",&n,&x);
     for(int t , h , m , i = 0 ; i < n ; ++i){
       scanf("%d%d%d" , &t , &h , &m);
       ve.push_back({m , {h , t }});
     }
     x1 = x ;
     sort(ve.rbegin() , ve.rend());
  /*   for(int i = 0 ; i < n ; ++i)
       cout<<ve[i].first<<" "<<ve[i].second.first<<" "<<ve[i].second.second<<" "<<i<<endl;
       */
       ans = solve(0) ;
       x = x1 ;
       memset(visit , 0 , sizeof(visit)) ;
       ans1 =solve(1);
       cout<<max(ans , ans1);
       return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vs vector<string>
#define vll vector<ll>
#define ii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define ld long double
#define iii tuple<int, int, int>
#define sc scanf
#define pb push_back
#define FOR(i, x, n) for(int i=x; i<=n; ++i)
#define ROF(i, x, n) for(int i=x; i>=n; --i)
#define MOD 1000000007
#define F first
#define S second
#define all(x) x.begin(), x.end()

priority_queue<ii> pq0, pq1;
vii v0, v1;

int f(int pr, int cur) {

    int ans = 0;
    vii x;

    FOR(i, 0, (int) v0.size() - 1) pq0.push(v0[i]);
    FOR(i, 0, (int) v1.size() - 1) pq1.push(v1[i]);

    while(1) {

        if(pq0.empty() && pq1.empty()) break;

        if(pr) {
            bool flag = 1;
            while(!pq0.empty()) {
                auto [u, v] = pq0.top();
                pq0.pop();
                if(v <= cur) {
                    cur += u;
                    pr = 0;
                    flag = 0;
                    ans++;
                    break;
                }
                else {
                    x.pb({u, v});
                }
            }
            if(flag) return ans;

            FOR(i, 0, (int) x.size()-1) {
                pq0.push(x[i]);
            }
            x.clear();
        }
        else {
            bool flag = 1;
            while(!pq1.empty()) {
                auto [u, v] = pq1.top();
                pq1.pop();
                if(v <= cur) {
                    cur += u;
                    pr = 1;
                    ans++;
                    flag = 0;
                    break;
                }
                else {
                    x.pb({u, v});
                }
            }
            if(flag) return ans;
            FOR(i, 0, (int) x.size() - 1) {
                pq1.push(x[i]);
            }
            x.clear();
        }
    }
    return ans;
}

int main() {

    int n, x;
    sc("%d %d", &n, &x);

    FOR(i, 1, n) {
        int t, m, h;
        sc("%d %d %d", &t, &h, &m);

        if(t) {
            v1.pb({m, h});
        }
        else {
            v0.pb({m, h});
        }
    }

    int ans1 = f(0, x);
    while(!pq0.empty()) pq0.pop();
    while(!pq1.empty()) pq1.pop();
    int ans2 = f(1, x);

    printf("%d\n", max(ans1, ans2));
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vs vector<string>
#define vll vector<ll>
#define ii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define ld long double
#define iii tuple<int, int, int>
#define sc scanf
#define pb push_back
#define FOR(i, x, n) for(int i=x; i<=n; ++i)
#define ROF(i, x, n) for(int i=x; i>=n; --i)
#define MOD 1000000007
#define F first
#define S second
#define all(x) x.begin(), x.end()

vii a, b;
bool compare(ii x, ii y) {
    return x.F == y.F ? x.S > y.S : x.F < y.F;
}

int main() {

    int n, x;
    sc("%d %d", &n, &x);

    FOR(i, 1, n) {
        int t, m, h;
        sc("%d %d %d", &t, &h, &m);

        if(t) {
            a.pb({h, m});
        }
        else {
            b.pb({h, m});
        }
    }

    sort(all(a), compare);
    sort(all(b), compare);

    int in1 = 0, in2 = 0, cur = x, ans1 = 0, pr = 'a', sza = (int) a.size(), szb = (int) b.size(), ans2 = 0;

    while(1) {

        if(pr == 'a') {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans1++;
                pr = 'b';
            }
            else {
                break;
            }
        }
        else {
            if(in1 < sza && b[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans1++;
                pr = 'a';
            }
            else {
                break;
            }
        }
    }

    in1 = in2 = 0, cur = x;
    pr = 'b';

    while(1) {
        if(pr == 'b') {
            if(in1 < sza && a[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans2++;
                pr = 'a';
            }
            else {
                break;
            }
        }
        else {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans2++;
                pr = 'b';
            }
            else {
                break;
            }
        }
    }

    printf("%d\n", max(ans1, ans2));
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vs vector<string>
#define vll vector<ll>
#define ii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define ld long double
#define iii tuple<int, int, int>
#define sc scanf
#define pb push_back
#define FOR(i, x, n) for(int i=x; i<=n; ++i)
#define ROF(i, x, n) for(int i=x; i>=n; --i)
#define MOD 1000000007
#define F first
#define S second
#define all(x) x.begin(), x.end()

vii a, b;
bool compare(ii x, ii y) {
    return x.F == y.F ? x.S > y.S : x.F < y.F;
}

int main() {

    int n, x;
    sc("%d %d", &n, &x);

    FOR(i, 1, n) {
        int t, m, h;
        sc("%d %d %d", &t, &h, &m);

        if(t) {
            a.pb({h, m});
        }
        else {
            b.pb({h, m});
        }
    }

    sort(all(a), compare);
    sort(all(b)), compare;

    int in1 = 0, in2 = 0, cur = x, ans1 = 0, pr = 'a', sza = (int) a.size(), szb = (int) b.size(), ans2 = 0;

    while(1) {

        if(pr == 'a') {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans1++;
                pr = 'b';
            }
            else {
                break;
            }
        }
        else {
            if(in1 < sza && b[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans1++;
                pr = 'a';
            }
            else {
                break;
            }
        }
    }

    in1 = in2 = 0, cur = x;
    pr = 'b';

    while(1) {
        if(pr == 'b') {
            if(in1 < sza && b[in1].F <= cur) {
                cur += b[in1].S;
                in1++;
                ans2++;
                pr = 'a';
            }
            else {
                break;
            }
        }
        else {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans2++;
                pr = 'b';
            }
            else {
                break;
            }
        }
    }

    printf("%d\n", max(ans1, ans2));
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vs vector<string>
#define vll vector<ll>
#define ii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define ld long double
#define iii tuple<int, int, int>
#define sc scanf
#define pb push_back
#define FOR(i, x, n) for(int i=x; i<=n; ++i)
#define ROF(i, x, n) for(int i=x; i>=n; --i)
#define MOD 1000000007
#define F first
#define S second
#define all(x) x.begin(), x.end()

vii a, b;
bool compare(ii x, ii y) {
    return x.F == y.F ? x.S > y.S : x.F < y.F;
}

int main() {

    int n, x;
    sc("%d %d", &n, &x);

    FOR(i, 1, n) {
        int t, m, h;
        sc("%d %d %d", &t, &h, &m);

        if(t) {
            a.pb({h, m});
        }
        else {
            b.pb({h, m});
        }
    }

    sort(all(a), compare);
    sort(all(b), compare);

    int in1 = 0, in2 = 0, cur = x, ans1 = 0, pr = 'a', sza = (int) a.size(), szb = (int) b.size(), ans2 = 0;

    while(1) {

        if(pr == 'a') {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans1++;
                pr = 'b';
            }
            else {
                break;
            }
        }
        else {
            if(in1 < sza && a[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans1++;
                pr = 'a';
            }
            else {
                break;
            }
        }
    }

    in1 = in2 = 0, cur = x;
    pr = 'b';

    while(1) {
        if(pr == 'b') {
            if(in1 < sza && a[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans2++;
                pr = 'a';
            }
            else {
                break;
            }
        }
        else {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans2++;
                pr = 'b';
            }
            else {
                break;
            }
        }
    }

    printf("%d\n", max(ans1, ans2));
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vs vector<string>
#define vll vector<ll>
#define ii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>
#define ld long double
#define iii tuple<int, int, int>
#define sc scanf
#define pb push_back
#define FOR(i, x, n) for(int i=x; i<=n; ++i)
#define ROF(i, x, n) for(int i=x; i>=n; --i)
#define MOD 1000000007
#define F first
#define S second
#define all(x) x.begin(), x.end()

vii a, b;
bool compare(ii x, ii y) {
    return x.F == y.F ? x.S > y.S : x.F < y.F;
}

int main() {

    int n, x;
    sc("%d %d", &n, &x);

    FOR(i, 1, n) {
        int t, m, h;
        sc("%d %d %d", &t, &h, &m);

        if(t) {
            a.pb({h, m});
        }
        else {
            b.pb({h, m});
        }
    }

    sort(all(a), compare);
    sort(all(b)), compare;

    int in1 = 0, in2 = 0, cur = x, ans1 = 0, pr = 'a', sza = (int) a.size(), szb = (int) b.size(), ans2 = 0;

    while(1) {

        if(pr == 'a') {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans1++;
                pr = 'b';
            }
            else {
                break;
            }
        }
        else {
            if(in1 < sza && b[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans1++;
                pr = 'a';
            }
            else {
                break;
            }
        }
    }

    in1 = in2 = 0, cur = x;
    pr = 'b';

    while(1) {
        if(pr == 'b') {
            if(in1 < sza && a[in1].F <= cur) {
                cur += a[in1].S;
                in1++;
                ans2++;
                pr = 'a';
            }
            else {
                break;
            }
        }
        else {
            if(in2 < szb && b[in2].F <= cur) {
                cur += b[in2].S;
                in2++;
                ans2++;
                pr = 'b';
            }
            else {
                break;
            }
        }
    }

    printf("%d\n", max(ans1, ans2));
}


#include <bits/stdc++.h>
#define ll long long
#define _r  freopen("in.txt", "r", stdin);
#define _w  freopen("out.txt", "w", stdout);
using namespace std;

vector< pair<int,int> > type1, type2;

int n,h;

int best_fit1()
{
    for(int i=0;i<type1.size();i++)
    {
        if(type1[i].first == -1) continue;
        if(type1[i].second <= h) {
            return i;
        }
    }
    return -1;
}
int best_fit2()
{
    for(int i=0;i<type2.size();i++)
    {
        if(type2[i].first == -1) continue;
        if(type2[i].second <= h) {
            return i;
        }
    }
    return -1;
}
int main()
{
   // _r
    cin>>n>>h;
    for(int i=0;i<n;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x==0) type1.push_back( {z,y} );
        else type2.push_back( {z,y} );
    }
    sort(type1.begin(), type1.end());
    reverse(type1.begin(), type1.end());

    sort(type2.begin(), type2.end());
    reverse(type2.begin(), type2.end());

    int res = 0;
    int idx1 = best_fit1(), idx2 = best_fit2();
    int res1 = (idx1==-1?-1:type1[idx1].first);
    int res2 = (idx2==-1?-1:type2[idx2].first);
    if(res1==-1 && res2==-1) return cout<<0<<endl,0;
    int next_type;

    if( idx2==-1 || res1>res2)
        res++, h+=res1, next_type = 2, type1[idx1] = {-1,-1};
    else
        res++, h+=res2, next_type = 1, type2[idx2] = {-1,-1} ;

    while(1)
    {
        if(next_type == 1)
        {
            int i = best_fit1();
            if(i == -1) break;
            h += type1[i].first;
            res++;
            next_type = 2;
            type1[i] = {-1,-1};
        }
        else{
            int i = best_fit2();
            if(i == -1) break;
            h += type2[i].first;
            res++;
            next_type = 1;
            type2[i] = {-1,-1};
        }
    }
    cout<<res<<endl;

}


#include <bits/stdc++.h>
#define ll long long
#define _r  freopen("in.txt", "r", stdin);
#define _w  freopen("out.txt", "w", stdout);
using namespace std;

vector< pair<int,int> > type[2];

vector< pair<int,int> > temp_type[2];
int n,h;

int best_fit(int t)
{
    for(int i=0;i<type[t].size();i++)
    {
        if(type[t][i].first == -1) continue;
        if(type[t][i].second <= h) {
            return i;
        }
    }
    return -1;
}
int main()
{
   // _r
    cin>>n>>h;

    int temp_h = h;
    for(int i=0;i<n;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x==0) type[0].push_back( {z,y} );
        else type[1].push_back( {z,y} );
    }
    sort(type[0].begin(), type[0].end());
    reverse(type[0].begin(), type[0].end());

    sort(type[1].begin(), type[1].end());
    reverse(type[1].begin(), type[1].end());

    temp_type[0] = type[0];
    temp_type[1] = type[1];

    int res[2] = {0,0};
    int next_type[2] = {1,2};

    for(int j=0;j<2;j++)
    {
        while(1)
        {
            if(next_type[j] == 1)
            {
                int i = best_fit(0);
                if(i == -1) break;
                h += type[0][i].first;
                res[j]++;
                next_type[j] = 2;
                type[0][i] = {-1,-1};
            }
            else{
                int i = best_fit(1);
                if(i == -1) break;
                h += type[1][i].first;
                res[j]++;
                next_type[j] = 1;
                type[1][i] = {-1,-1};
            }
        }
        h = temp_h;
        type[0] = temp_type[0];
        type[1] = temp_type[1];
    }

    cout<<max(res[0], res[1])<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define _r  freopen("in.txt", "r", stdin);
#define _w  freopen("out.txt", "w", stdout);
using namespace std;

vector< pair<int,int> > type[2];

int n,h;

int best_fit(int t)
{
    for(int i=0;i<type[t].size();i++)
    {
        if(type[t][i].first == -1) continue;
        if(type[t][i].second <= h) {
            return i;
        }
    }
    return -1;
}
int main()
{
   // _r
    cin>>n>>h;
    for(int i=0;i<n;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x==0) type[0].push_back( {z,y} );
        else type[1].push_back( {z,y} );
    }
    sort(type[0].begin(), type[0].end());
    reverse(type[0].begin(), type[0].end());

    sort(type[1].begin(), type[1].end());
    reverse(type[1].begin(), type[1].end());
    int res = 0;
    int next_type = -1;

    while(1)
    {
        if(next_type == -1)
        {
            int idx1 = best_fit(0), idx2 = best_fit(1);
            if(idx1==-1 && idx2==-1) break;
            int res1 = (idx1==-1?-1:type[0][idx1].first);
            int res2 = (idx2==-1?-1:type[1][idx2].first);
            if(res2 == -1)
                res++, h+=res1, next_type = 2, type[0][idx1] = {-1,-1};
            else if(res1 == -1)
                res++, h+=res2, next_type = 1, type[1][idx2] = {-1,-1};
            else {
                if(res1>res2)
                    res++, h+=res1, next_type = 2, type[0][idx1] = {-1,-1};
                else
                    res++, h+=res2, next_type = 1, type[1][idx2] = {-1,-1};
            }
        }
        else if(next_type == 1)
        {
            int i = best_fit(0);
            if(i == -1) break;
            h += type[0][i].first;
            res++;
            next_type = 2;
            type[0][i] = {-1,-1};
        }
        else{
            int i = best_fit(1);
            if(i == -1) break;
            h += type[1][i].first;
            res++;
            next_type = 1;
            type[1][i] = {-1,-1};
        }
    }
    cout<<res<<endl;

}


#include <bits/stdc++.h>
#define ll long long
#define _r  freopen("in.txt", "r", stdin);
#define _w  freopen("out.txt", "w", stdout);
using namespace std;

vector< pair<int,int> > type[2];

vector< pair<int,int> > temp_type[2];
int n,h;

int best_fit(int t)
{
    for(int i=0;i<type[t].size();i++)
    {
        if(type[t][i].first == -1) continue;
        if(type[t][i].second <= h) {
            return i;
        }
    }
    return -1;
}
int main()
{
   // _r
    cin>>n>>h;
    for(int i=0;i<n;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x==0) type[0].push_back( {z,y} );
        else type[1].push_back( {z,y} );
    }
    sort(type[0].begin(), type[0].end());
    reverse(type[0].begin(), type[0].end());

    sort(type[1].begin(), type[1].end());
    reverse(type[1].begin(), type[1].end());
    temp_type[0] = type[0];
    temp_type[1] = type[1];

    int res[2] = {0,0};
    int next_type[2] = {1,2};

    for(int j=0;j<2;j++)
    {
        while(1)
        {
             if(next_type[j] == 1)
            {
                int i = best_fit(0);
                if(i == -1) break;
                h += type[0][i].first;
                res[j]++;
                next_type[j] = 2;
                type[0][i] = {-1,-1};
            }
            else{
                int i = best_fit(1);
                if(i == -1) break;
                h += type[1][i].first;
                res[j]++;
                next_type[j] = 1;
                type[1][i] = {-1,-1};
            }
        }
        type[0] = temp_type[0];
        type[1] = temp_type[1];
    }

    cout<<max(res[0], res[1])<<endl;
}


//FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M=1000000007;
const ll MM=998244353;
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";

ll newmod(ll a,ll b)
 {
  return ((a%b)+b)%b;
}

ll powM(ll a,ll b,ll m )
{
  if(a<=0)return 0;
  a%=m;
  if(!b)return 1;
  ll ans=1LL;
  while(b)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}

ll poww(ll a,ll b)
{

  ll ans=1;
  while(b)
  {
    if(b&1)ans=ans*a;
    a=a*a;
    b>>=1;
  }

return ans;

}

template<typename T,typename F>
void chmax( T &a,F b){
  if(b>a)a=b;
}

template<typename T,typename F>
void chmin( T &a,F b){
  if(b<a)a=b;
}

const ll N=2e5+5;

priority_queue<pll>o,z;

ll solve(ll type,ll x){

priority_queue<pll>oo=o,zz=z;

ll ans=0;
ll flag=0;

while(!flag){
  if(type){
    if(zz.empty()){
      flag=1;
      break;
    }
    auto p=zz.top();
    if(-p.ff>x){
      flag=1;
      break;
    }
    ans++;
    x+=p.ss;
    type=0;
    zz.pop();

  }
  else {
    if(oo.empty()){
      flag=1;
      break;
    }
    auto p=oo.top();
    if(-p.ff>x){
      flag=1;
      break;
    }
    ans++;
    x+=p.ss;
    type=1;
    oo.pop();
  }
}
return ans;
}

int main()
{

IOS;

ll n,x;
cin>>n>>x;

FOR(i,n)
{
  ll t,h,m;
  cin>>t>>h>>m;
  if(t)o.push({-h,m});//one.pb({h,m});
  else z.push({-h,m});  //zero.pb({h,m});
}

cout<<max(solve(1,x),solve(0,x));

cout<<endl;

// solve if initial type is 1.
//and then for ini type 0. take max then.

//end_routine();

return 0;

 }


//FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M=1000000007;
const ll MM=998244353;
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";

ll newmod(ll a,ll b)
 {
  return ((a%b)+b)%b;
}

ll powM(ll a,ll b,ll m )
{
  if(a<=0)return 0;
  a%=m;
  if(!b)return 1;
  ll ans=1LL;
  while(b)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}

ll poww(ll a,ll b)
{

  ll ans=1;
  while(b)
  {
    if(b&1)ans=ans*a;
    a=a*a;
    b>>=1;
  }

return ans;

}

template<typename T,typename F>
void chmax( T &a,F b){
  if(b>a)a=b;
}

template<typename T,typename F>
void chmin( T &a,F b){
  if(b<a)a=b;
}

const ll N=1e6+1;

ll solve(ll type,vpll t1,vpll t0,ll x){

ll ans=0;

ll flag=0;

while(!flag){
ll pos=-1,mx=0;
  if(!type){
    type=1;
    for(int i=0;i<sz(t0);i++){
      if(t0[i].ff<=x&&t0[i].ss>mx){
             pos=i;
             mx=max(mx,(ll)t0[i].ss);
      }
    }
        if(pos==-1){
      flag=1;
      break;
    }
    ans++;
    x+=mx;
    t0.erase(t0.begin()+pos);
  }
  else {
    type=0;
    ll pos=-1,mx=0;
    for(int i=0;i<sz(t1);i++){
      if(t1[i].ff<=x&&t1[i].ss>mx){
        mx=max(mx,(ll)t1[i].ss);
        pos=i;
      }
    }

    if(pos==-1){
      flag=1;
      break;
    }
    ans++;
    x+=mx;
    t1.erase(t1.begin()+pos);
  }

}
//cout<<x<<endl;

return ans;
}

int main()
{

IOS;

ll n,x;cin>>n>>x;

vpll t0,t1;

FOR(i,n){
  ll t;cin>>t;
  ll a,b;cin>>a>>b;
  if(t){
    t1.pb({a,b});
  }
  else {
t0.pb({a,b});
  }
}

cout<<max(solve(1,t1,t0,x),solve(0,t1,t0,x));

//end_routine();

return 0;

 }


//FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M=1000000007;
const ll MM=998244353;
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";

ll newmod(ll a,ll b)
 {
  return ((a%b)+b)%b;
}

ll powM(ll a,ll b,ll m )
{
  if(a<=0)return 0;
  a%=m;
  if(!b)return 1;
  ll ans=1LL;
  while(b)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}

ll poww(ll a,ll b)
{

  ll ans=1;
  while(b)
  {
    if(b&1)ans=ans*a;
    a=a*a;
    b>>=1;
  }

return ans;

}

template<typename T,typename F>
void chmax( T &a,F b){
  if(b>a)a=b;
}

template<typename T,typename F>
void chmin( T &a,F b){
  if(b<a)a=b;
}

const ll N=2e5+5;

priority_queue<pll>o,z;

ll solve(ll type,ll x){

priority_queue<pll>oo=o,zz=z;

ll ans=0;
ll flag=0;

while(!flag){
  if(type){
    if(zz.empty()){
      flag=1;
      break;
    }
    auto p=zz.top();
      zz.pop();
    if(-p.ff>x){
      flag=1;
      break;
    }
    ans++;
    x+=p.ss;
    type=0;

  }
  else {
    if(oo.empty()){
      flag=1;
      break;
    }
    auto p=oo.top();
      oo.pop();
    if(-p.ff>x){
      flag=1;
      break;
    }
    ans++;
    x+=p.ss;
    type=1;

  }
}
return ans;
}

int main()
{

IOS;

ll n,x;
cin>>n>>x;

FOR(i,n)
{
  ll t,h,m;
  cin>>t>>h>>m;
  if(t)o.push({-h,m});//one.pb({h,m});
  else z.push({-h,m});  //zero.pb({h,m});
}
/*
while(!z.empty()){
  d2(z.top().ff,z.top().ss);
  z.pop();
}*/

cout<<max(solve(1,x),solve(0,x));

cout<<endl;

// solve if initial type is 1.
//and then for ini type 0. take max then.

//end_routine();

return 0;

 }


#include <bits/stdc++.h>

/*
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <ctype.h>
*/

using namespace std;

typedef pair<int , int > pii;
typedef pair<long , long> pll;
typedef pair<pii , int > piii;
typedef pair<pll , long > plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector <pii> vii;
typedef vector<pll> vll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
typedef long long int lli;
typedef float f;
typedef char ch;
typedef pair<int,ll> pill;

#define loop(n) for(int i = 0; i < (n); i++)
#define lp(x , s , e) for(int x = (s); x < (e); x++)
#define lpe(x , s , e) for(int x = (s); x <= (e); x++)
#define readline(s) getline(cin,s)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define clr(x , val) memset((x) , (val) , sizeof(x))
#define Unique(n)   (n).erase(unique(all(n)), (n).end())
#define endl '\n'

ll power(ll a, ll b)
{
    ll res = 1;
    for(int i = 0; i<b; i++)
       res *= a;

    return res;
}

struct candy
{
	int h , m;
};
struct compare
{
	bool operator()(candy const& a1, candy const& a2)
	{
		return a1.m < a2.m;
	}
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n , x , t , h , m , tmpx;
    cin >> n >> x;
    tmpx = x;
    priority_queue <candy ,vector <candy>, compare> p1 , p2 , p3 , p4 , tmp1 , tmp2 , tmp3 , tmp4;
    candy temp;
    loop(n)
    {
    	cin >> t >> h >> m;
    	if (!t)
    	{
    		temp.h = h;
    		temp.m = m;
    		p1.push(temp);
    		p3.push(temp);
    	}
    	else
    	{
    		temp.h = h;
    		temp.m = m;
    		p2.push(temp);
    		p4.push(temp);
    	}
    }
   /* while (!p1.empty())
    {
    	cout << p1.top().h << " " << p1.top().m << endl;
    	p1.pop();
    }
    while (!p2.empty())
	{
		cout << p2.top().h << " " << p2.top().m << endl;
		p2.pop();
	}*/
    /*int maxi1 = 0, maxi2 = 0 , flag , ans = 0;
    while (!p1.empty())
	{
		if (x >= p1.top().h)
		{
			maxi1 = p1.top().m;
			break;
		}
		else
		{
			tmp1.push(p1.top());
			p1.pop();
		}
	}
    while (!p2.empty())
	{
		if (x >= p2.top().h)
		{
			maxi2 = p2.top().m;
			break;
		}
		else
		{
			tmp2.push(p2.top());
			p2.pop();
		}
	}
    if (maxi1 == maxi2 && maxi1 == 0)
    {
    	return cout << 0 , 0;
    }
    if (maxi1 > maxi2)
    {
    	x += maxi1;
    	flag = 1;
    	ans++;
    	p1.pop();
    }
    else if (maxi2 > maxi1)
    {
    	x += maxi2;
    	flag = 2;
    	ans++;
    	p2.pop();
    }
    else
    {

    }
    while (!tmp1.empty())
	{
		p1.push(tmp1.top());
		tmp1.pop();
	}
	while (!tmp2.empty())
	{
		p2.push(tmp2.top());
		tmp2.pop();
	}*/
    int flag = 1, ans = 0 , ans2 = 0;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
		    while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
		    flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
		    while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
		    flag++;
		}
	}
	flag = 2;
	x  = tmpx;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p3.empty())
			{
				break;
			}
			while (!p3.empty())
			{
				if (x >= p3.top().h)
				{
					x += p3.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp3.push(p3.top());
					p3.pop();
				}
			}
			if (p3.empty())
				break;
			p3.pop();
			while (!tmp3.empty())
			{
				p3.push(tmp3.top());
				tmp3.pop();
			}
			flag++;
		}
		else
		{
			if (p4.empty())
				break;
			while (!p4.empty())
			{
				if (x >= p4.top().h)
				{
					x += p4.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp4.push(p4.top());
					p4.pop();
				}
			}
			if (p4.empty())
				break;
			p4.pop();
			while (!tmp4.empty())
			{
				p4.push(tmp4.top());
				tmp4.pop();
			}
			flag++;
		}
	}
	cout << max(ans , ans2);
	return 0;
}


#include <bits/stdc++.h>

/*
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <ctype.h>
*/

using namespace std;

typedef pair<int , int > pii;
typedef pair<long , long> pll;
typedef pair<pii , int > piii;
typedef pair<pll , long > plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector <pii> vii;
typedef vector<pll> vll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
typedef long long int lli;
typedef float f;
typedef char ch;
typedef pair<int,ll> pill;

#define loop(n) for(int i = 0; i < (n); i++)
#define lp(x , s , e) for(int x = (s); x < (e); x++)
#define lpe(x , s , e) for(int x = (s); x <= (e); x++)
#define readline(s) getline(cin,s)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define clr(x , val) memset((x) , (val) , sizeof(x))
#define Unique(n)   (n).erase(unique(all(n)), (n).end())
#define endl '\n'

ll power(ll a, ll b)
{
    ll res = 1;
    for(int i = 0; i<b; i++)
       res *= a;

    return res;
}

struct candy
{
	int h , m;
};
struct compare
{
	bool operator()(candy const& a1, candy const& a2)
	{
		return a1.m < a2.m;
	}
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n , x , t , h , m;
    cin >> n >> x;
    priority_queue <candy ,vector <candy>, compare> p1 , p2 , p3 , p4 , tmp1 , tmp2 , tmp3 , tmp4;
    candy temp;
    loop(n)
    {
    	cin >> t >> h >> m;
    	if (!t)
    	{
    		temp.h = h;
    		temp.m = m;
    		p1.push(temp);
    		p3.push(temp);
    	}
    	else
    	{
    		temp.h = h;
    		temp.m = m;
    		p2.push(temp);
    		p4.push(temp);
    	}
    }
   /* while (!p1.empty())
    {
    	cout << p1.top().h << " " << p1.top().m << endl;
    	p1.pop();
    }
    while (!p2.empty())
	{
		cout << p2.top().h << " " << p2.top().m << endl;
		p2.pop();
	}*/
    /*int maxi1 = 0, maxi2 = 0 , flag , ans = 0;
    while (!p1.empty())
	{
		if (x >= p1.top().h)
		{
			maxi1 = p1.top().m;
			break;
		}
		else
		{
			tmp1.push(p1.top());
			p1.pop();
		}
	}
    while (!p2.empty())
	{
		if (x >= p2.top().h)
		{
			maxi2 = p2.top().m;
			break;
		}
		else
		{
			tmp2.push(p2.top());
			p2.pop();
		}
	}
    if (maxi1 == maxi2 && maxi1 == 0)
    {
    	return cout << 0 , 0;
    }
    if (maxi1 > maxi2)
    {
    	x += maxi1;
    	flag = 1;
    	ans++;
    	p1.pop();
    }
    else if (maxi2 > maxi1)
    {
    	x += maxi2;
    	flag = 2;
    	ans++;
    	p2.pop();
    }
    else
    {

    }
    while (!tmp1.empty())
	{
		p1.push(tmp1.top());
		tmp1.pop();
	}
	while (!tmp2.empty())
	{
		p2.push(tmp2.top());
		tmp2.pop();
	}*/
    int flag = 1, ans = 0 , ans2 = 0;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
		    while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
		    flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
		    while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
		    flag++;
		}
	}
	flag = 2;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p3.empty())
			{
				break;
			}
			while (!p3.empty())
			{
				if (x >= p3.top().h)
				{
					x += p3.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp3.push(p3.top());
					p3.pop();
				}
			}
			if (p3.empty())
				break;
			p3.pop();
			while (!tmp3.empty())
			{
				p3.push(tmp3.top());
				tmp3.pop();
			}
			flag++;
		}
		else
		{
			if (p4.empty())
				break;
			while (!p4.empty())
			{
				if (x >= p4.top().h)
				{
					x += p4.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp4.push(p4.top());
					p4.pop();
				}
			}
			if (p4.empty())
				break;
			p4.pop();
			while (!tmp4.empty())
			{
				p4.push(tmp4.top());
				tmp4.pop();
			}
			flag++;
		}
	}
	cout << max(ans , ans2);
	return 0;
}


#include <bits/stdc++.h>

/*
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <ctype.h>
*/

using namespace std;

typedef pair<int , int > pii;
typedef pair<long , long> pll;
typedef pair<pii , int > piii;
typedef pair<pll , long > plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector <pii> vii;
typedef vector<pll> vll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
typedef long long int lli;
typedef float f;
typedef char ch;
typedef pair<int,ll> pill;

#define loop(n) for(int i = 0; i < (n); i++)
#define lp(x , s , e) for(int x = (s); x < (e); x++)
#define lpe(x , s , e) for(int x = (s); x <= (e); x++)
#define readline(s) getline(cin,s)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define clr(x , val) memset((x) , (val) , sizeof(x))
#define Unique(n)   (n).erase(unique(all(n)), (n).end())
#define endl '\n'

ll power(ll a, ll b)
{
    ll res = 1;
    for(int i = 0; i<b; i++)
       res *= a;

    return res;
}

struct candy
{
	int h , m;
};
struct compare
{
	bool operator()(candy const& a1, candy const& a2)
	{
		return a1.m < a2.m;
	}
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n , x , t , h , m;
    cin >> n >> x;
    priority_queue <candy ,vector <candy>, compare> p1 , p2 , tmp1 , tmp2;
    candy temp;
    loop(n)
    {
    	cin >> t >> h >> m;
    	if (!t)
    	{
    		temp.h = h;
    		temp.m = m;
    		p1.push(temp);
    	}
    	else
    	{
    		temp.h = h;
    		temp.m = m;
    		p2.push(temp);
    	}
    }
   /* while (!p1.empty())
    {
    	cout << p1.top().h << " " << p1.top().m << endl;
    	p1.pop();
    }
    while (!p2.empty())
	{
		cout << p2.top().h << " " << p2.top().m << endl;
		p2.pop();
	}*/
    int maxi1 = 0, maxi2 = 0 , flag , ans = 0;
    while (!p1.empty())
	{
		if (x >= p1.top().h)
		{
			maxi1 = p1.top().m;
			break;
		}
		else
		{
			tmp1.push(p1.top());
			p1.pop();
		}
	}
    while (!p2.empty())
	{
		if (x >= p2.top().h)
		{
			maxi2 = p2.top().m;
			break;
		}
		else
		{
			tmp2.push(p2.top());
			p2.pop();
		}
	}
    if (maxi1 == maxi2 && maxi1 == 0)
    {
    	return cout << 0 , 0;
    }
    if (maxi1 >= maxi2)
    {
    	x += maxi1;
    	flag = 1;
    	ans++;
    	p1.pop();
    }
    else
    {
    	x += maxi2;
    	flag = 2;
    	ans++;
    	p2.pop();
    }
    while (!tmp1.empty())
	{
		p1.push(tmp1.top());
		tmp1.pop();
	}
	while (!tmp2.empty())
	{
		p2.push(tmp2.top());
		tmp2.pop();
	}
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
		    while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
		    flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
		    while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
		    flag++;
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

/*
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <ctype.h>
*/

using namespace std;

typedef pair<int , int > pii;
typedef pair<long , long> pll;
typedef pair<pii , int > piii;
typedef pair<pll , long > plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector <pii> vii;
typedef vector<pll> vll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
typedef long long int lli;
typedef float f;
typedef char ch;
typedef pair<int,ll> pill;

#define loop(n) for(int i = 0; i < (n); i++)
#define lp(x , s , e) for(int x = (s); x < (e); x++)
#define lpe(x , s , e) for(int x = (s); x <= (e); x++)
#define readline(s) getline(cin,s)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define clr(x , val) memset((x) , (val) , sizeof(x))
#define Unique(n)   (n).erase(unique(all(n)), (n).end())
#define endl '\n'

ll power(ll a, ll b)
{
    ll res = 1;
    for(int i = 0; i<b; i++)
       res *= a;

    return res;
}

struct candy
{
	int h , m;
};
struct compare
{
	bool operator()(candy const& a1, candy const& a2)
	{
		return a1.m < a2.m;
	}
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n , x , t , h , m;
    cin >> n >> x;
    priority_queue <candy ,vector <candy>, compare> p1 , p2 , tmp1 , tmp2;
    candy temp;
    loop(n)
    {
    	cin >> t >> h >> m;
    	if (!t)
    	{
    		temp.h = h;
    		temp.m = m;
    		p1.push(temp);
    	}
    	else
    	{
    		temp.h = h;
    		temp.m = m;
    		p2.push(temp);
    	}
    }
   /* while (!p1.empty())
    {
    	cout << p1.top().h << " " << p1.top().m << endl;
    	p1.pop();
    }
    while (!p2.empty())
	{
		cout << p2.top().h << " " << p2.top().m << endl;
		p2.pop();
	}*/
    int maxi1 = 0, maxi2 = 0 , flag , ans = 0;
    while (!p1.empty())
	{
		if (x >= p1.top().h)
		{
			maxi1 = p1.top().m;
			break;
		}
		else
		{
			tmp1.push(p1.top());
			p1.pop();
		}
	}
    while (!p2.empty())
	{
		if (x >= p2.top().h)
		{
			maxi2 = p2.top().m;
			break;
		}
		else
		{
			tmp2.push(p2.top());
			p2.pop();
		}
	}
    if (maxi1 == maxi2 && maxi1 == 0)
    {
    	return cout << 0 , 0;
    }
    if (maxi1 > maxi2)
    {
    	x += maxi1;
    	flag = 1;
    	ans++;
    	p1.pop();
    }
    else
    {
    	x += maxi2;
    	flag = 2;
    	ans++;
    	p2.pop();
    }
    while (!tmp1.empty())
	{
		p1.push(tmp1.top());
		tmp1.pop();
	}
	while (!tmp2.empty())
	{
		p2.push(tmp2.top());
		tmp2.pop();
	}
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
		    while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
		    flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
		    while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
		    flag++;
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

/*
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <ctype.h>
*/

using namespace std;

typedef pair<int , int > pii;
typedef pair<long , long> pll;
typedef pair<pii , int > piii;
typedef pair<pll , long > plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector <pii> vii;
typedef vector<pll> vll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
typedef long long int lli;
typedef float f;
typedef char ch;
typedef pair<int,ll> pill;

#define loop(n) for(int i = 0; i < (n); i++)
#define lp(x , s , e) for(int x = (s); x < (e); x++)
#define lpe(x , s , e) for(int x = (s); x <= (e); x++)
#define readline(s) getline(cin,s)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define clr(x , val) memset((x) , (val) , sizeof(x))
#define Unique(n)   (n).erase(unique(all(n)), (n).end())
#define endl '\n'

ll power(ll a, ll b)
{
    ll res = 1;
    for(int i = 0; i<b; i++)
       res *= a;

    return res;
}

struct candy
{
	int h , m;
};
struct compare
{
	bool operator()(candy const& a1, candy const& a2)
	{
		return a1.m < a2.m;
	}
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n , x , t , h , m;
    cin >> n >> x;
    priority_queue <candy ,vector <candy>, compare> p1 , p2 , tmp1 , tmp2;
    candy temp;
    loop(n)
    {
    	cin >> t >> h >> m;
    	if (!t)
    	{
    		temp.h = h;
    		temp.m = m;
    		p1.push(temp);
    	}
    	else
    	{
    		temp.h = h;
    		temp.m = m;
    		p2.push(temp);
    	}
    }
   /* while (!p1.empty())
    {
    	cout << p1.top().h << " " << p1.top().m << endl;
    	p1.pop();
    }
    while (!p2.empty())
	{
		cout << p2.top().h << " " << p2.top().m << endl;
		p2.pop();
	}*/
    /*int maxi1 = 0, maxi2 = 0 , flag , ans = 0;
    while (!p1.empty())
	{
		if (x >= p1.top().h)
		{
			maxi1 = p1.top().m;
			break;
		}
		else
		{
			tmp1.push(p1.top());
			p1.pop();
		}
	}
    while (!p2.empty())
	{
		if (x >= p2.top().h)
		{
			maxi2 = p2.top().m;
			break;
		}
		else
		{
			tmp2.push(p2.top());
			p2.pop();
		}
	}
    if (maxi1 == maxi2 && maxi1 == 0)
    {
    	return cout << 0 , 0;
    }
    if (maxi1 > maxi2)
    {
    	x += maxi1;
    	flag = 1;
    	ans++;
    	p1.pop();
    }
    else if (maxi2 > maxi1)
    {
    	x += maxi2;
    	flag = 2;
    	ans++;
    	p2.pop();
    }
    else
    {

    }
    while (!tmp1.empty())
	{
		p1.push(tmp1.top());
		tmp1.pop();
	}
	while (!tmp2.empty())
	{
		p2.push(tmp2.top());
		tmp2.pop();
	}*/
    int flag = 1, ans = 0 , ans2 = 0;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
		    while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
		    flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
		    while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
		    flag++;
		}
	}
	flag = 2;
	while (true)
	{
		if (flag % 2 == 0)
		{
			if (p1.empty())
			{
				break;
			}
			while (!p1.empty())
			{
				if (x >= p1.top().h)
				{
					x += p1.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp1.push(p1.top());
					p1.pop();
				}
			}
			if (p1.empty())
				break;
			p1.pop();
			while (!tmp1.empty())
			{
				p1.push(tmp1.top());
				tmp1.pop();
			}
			flag++;
		}
		else
		{
			if (p2.empty())
				break;
			while (!p2.empty())
			{
				if (x >= p2.top().h)
				{
					x += p2.top().m;
					ans2++;
					break;
				}
				else
				{
					tmp2.push(p2.top());
					p2.pop();
				}
			}
			if (p2.empty())
				break;
			p2.pop();
			while (!tmp2.empty())
			{
				p2.push(tmp2.top());
				tmp2.pop();
			}
			flag++;
		}
	}
	cout << max(ans , ans2);
	return 0;
}


#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;

template<class T> bool max_self(T & a, const T & b){
  return b > a ? a = b, true : false;
}
template<class T> bool min_self(T & a, const T & b){
  return b < a ? a = b, true : false;
}
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);

class candy{
  public:
  int height, weight;
  bool used = false;

  candy(int a, int b) : height(a), weight(b){}

  bool operator<(const candy &other){
    return weight > other.weight;
  }
};

int N, x, opt = 0;
array<vector<candy>, 2> info, util;

void solve(){
  for(int start = 0, cnt, reach, move; start <= 1; ++start){
    util = info, cnt = 0, reach = x, move = start;

    again:
    for(candy &cur : util[move]){
      if(cur.used) continue;
      else if(cur.height <= reach){
        reach += cur.weight;
        move = !move;
        cur.used = true;
        cnt++;
        goto again;
      }
    }

    max_self(opt, cnt);
  }

  cout << opt << '\n';
  return;
}

int main(){
  FAST_IO
  cin >> N >> x;
  for(int rep = 0, t, h, m; rep < N; ++rep){
    cin >> t >> h >> m;
    info[t].EB(h, m);
  }

  for(auto &t : info) sort(ALL(t));

  solve();
  return 0;
}


#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;

template<class T> bool max_self(T & a, const T & b){
  return b > a ? a = b, true : false;
}
template<class T> bool min_self(T & a, const T & b){
  return b < a ? a = b, true : false;
}
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);

int N, x, opt = 0, cur[2], lim[2];
array<vector<pair<int, int>>, 2> info;

void solve(){
  for(int type = 0, cnt, reach, move; type <= 1; ++type){
    cur[0] = cur[1] = cnt = 0;
    reach = x;
    move = type;

    while(cur[move] < lim[move] && info[move][cur[move]].F <= reach){
      reach += info[move][cur[move]].S;
      cur[move]++;
      cnt++;
      move = !move;
    }

    assert(cnt == cur[0] + cur[1]);
    max_self(opt, cnt);
  }

  cout << opt << '\n';
  return;
}

int main(){
  FAST_IO
  cin >> N >> x;
  for(int rep = 0, t, h, m; rep < N; ++rep){
    cin >> t >> h >> m;
    lim[t]++;
    info[t].PB(MP(h, m));
  }

  for(auto &t : info){
    sort(ALL(t), [](auto a, auto b){
                    if(a.F != b.F) return a.F < b.F;
                    return a.S > b.S;
                    });
  }

  solve();
  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int main()
{

    fio;
    ll k,i,j,u,v,t,x,y,n,l,r,m,h;

     cin>>n>>k;
     vector<pii>vec1,vec2;

     for(i=0;i<n;i++)
     {
     	ll type;
     	cin>>type>>h>>m;

     	if(type==0)
     	{
     	    vec1.pb(mp(h,m));

		 }
		 else
		 {
		 	vec2.pb(mp(h,m));

		 }

	 }
	 bool visited1[n+1]={false};
	 bool visited2[n+1]={false};

	 x=k;
	 ll ans1=0,ans2=0;

	 while(true)
	 {
	 	ll maxi=0,ind=-1;
	 	for(i=0;i<vec1.size();i++)
	 	{
	 		if(!visited1[i]&&(vec1[i].fi<=x))
	 		{
	 			if(vec1[i].se>maxi)
	 			{
	 				maxi=vec1[i].se;
	 				ind=i;
				 }

			 }

		 }
		 if(ind==-1)
		 break;
		 visited1[ind]=1;
		 x+=maxi;
		 ans1++;

		 	 maxi=0,ind=-1;
	 	for(i=0;i<vec2.size();i++)
	 	{
	 		if(!visited2[i]&&(vec2[i].fi<=x))
	 		{
	 			if(vec2[i].se>maxi)
	 			{
	 				maxi=vec2[i].se;
	 				ind=i;
				 }

			 }

		 }
		 if(ind==-1)
		 break;
		 visited2[ind]=1;
		 x+=maxi;
		 ans1++;

	 }
	 memset(visited1,false,sizeof(visited1));
	 memset(visited2,false,sizeof(visited2));
	 x=k;
	  while(true)
	 {

		 ll	 maxi=0,ind=-1;
	 	for(i=0;i<vec2.size();i++)
	 	{
	 		if(!visited2[i]&&(vec2[i].fi<=x))
	 		{
	 			if(vec2[i].se>maxi)
	 			{
	 				maxi=vec2[i].se;
	 				ind=i;
				 }

			 }

		 }
		 if(ind==-1)
		 break;
		 visited2[ind]=1;
		 x+=maxi;
		 ans2++;

	 		 maxi=0,ind=-1;
	 	for(i=0;i<vec1.size();i++)
	 	{
	 		if(!visited1[i]&&(vec1[i].fi<=x))
	 		{
	 			if(vec1[i].se>maxi)
	 			{
	 				maxi=vec1[i].se;
	 				ind=i;
				 }

			 }

		 }
		 if(ind==-1)
		 break;
		 visited1[ind]=1;
		 x+=maxi;
		 ans2++;

	 }
	 cout<<max(ans1,ans2);

}


// if you get a wrong answer but you are convinced
// that your logic is correct do :- 1) read the
// entire code and try to find if something needs
// to be added 2) start making test cases that you
// think can possibly give WA.
#include <bits/stdc++.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
using namespace std;
typedef double  dbl;
typedef long long int lld;
#define PI 3.14159265358979323
#define foi(i,n) for(lld i = 0; i < n; i+=1)
#define fod(i,n) for(lld i=n-1; i >=0; i-=1)
#define MAX std::numeric_limits<lld>::max()
#define MIN std::numeric_limits<lld>::min()
#define FastIO  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define auth set <lld> :: iterator
#define pb push_back
#define LEN 2010
lld H[LEN];
lld M[LEN];
lld first (set <lld> caramel, set <lld> fruit, lld n, lld x)
{
	lld cnt = 0;
	lld ght = x;
	while(true){
		lld pos = -1;
		if (cnt%2 == 0){
			lld ans = MIN;
			for(auth itr = caramel.begin(); itr != caramel.end(); itr++){
				if (H[*itr] <= ght){
					if (M[*itr] > ans){
						ans=M[*itr];
						pos=*itr;
					}
				}
			}
			if (pos == -1)
				break;
			else {ght += M[pos]; cnt++; caramel.erase(pos);}
		}
		else{
			lld ans = MIN;
			for(auth itr = fruit.begin(); itr != fruit.end(); itr++){
				if (H[*itr] <= ght){
					if (M[*itr] > ans){
						ans=M[*itr];
						pos=*itr;
					}
				}
			}
			if (pos == -1)
				break;
			else {ght += M[pos]; cnt++; fruit.erase(pos);}
		}
	}
	return cnt;
}
lld second (set <lld> caramel, set <lld> fruit, lld n, lld x)
{
	lld cnt = 0;
	lld ght = x;
	while(true){
		lld pos = -1;
		if (cnt%2 == 0){
			lld ans = MIN;
			for(auth itr = fruit.begin(); itr != fruit.end(); itr++){
				if (H[*itr] <= ght){
					if (M[*itr] > ans){
						ans=M[*itr];
						pos=*itr;
					}
				}
			}
			if (pos == -1)
				break;
			else {ght += M[pos]; cnt++; fruit.erase(pos);}
		}
		else{
			lld ans = MIN;
			for(auth itr = caramel.begin(); itr != caramel.end(); itr++){
				if (H[*itr] <= ght){
					if (M[*itr] > ans){
						ans=M[*itr];
						pos=*itr;
					}
				}
			}
			if (pos == -1)
				break;
			else {ght += M[pos]; cnt++; caramel.erase(pos);}
		}
	}
	return cnt;
}
int main ()
{
      #ifndef ONLINE_JUDGE
      freopen("inputf.in",  "r",  stdin);
      freopen("outputf.in", "w", stdout);
      #endif
      FastIO
      lld n, x;
      cin >> n >> x;
      set <lld> fruit, caramel;
      for(lld i = 0; i < n; i += 1){
      	lld a, b, c;
      	cin >> a >> b >> c;
      	if (a == 0) caramel.insert(i);
      	else fruit.insert(i);
      	H[i] = b; M[i] = c;
      }
      cout << max(first(caramel,fruit,n,x), second(caramel,fruit,n,x));
      return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

void fastio()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)
#define foback(i, n) for (int i = n - 1; i >= 0; --i)
#define fo1(i, n) for (ll i = 1; i <= n; ++i)

typedef long long ll;

void one();

int main()
{
    fastio();

    int t = 1;
    // cin >> t;
    fo(i, t)
    {
        // cout << "Case #" << (i+1) << ": ";
        one();
    }

    return 0;
}

void one()
{
    int n; cin>>n;
    int x; cin>>x;

    vector<int> t(n),h(n),m(n);

    fo(i,n) {
        cin>>t[i]>>h[i]>>m[i];
    }

    int ans=0;
    fo(jstart, 2) {
        int xx = x;

        vector<bool> was(n);
    int cnt=0;
        int cur[2] = {0,0};

        fo(i,n) {
            int type = (jstart + i) % 2;

            int k = -1;
            fo(j,n) {
                if (!was[j] && h[j]<=xx && t[j]==type) {
                    if (k < 0 || m[k]<m[j]) {
                        k=j;
                    }
                }
            }

            if (k >= 0) {
                cnt++;
                xx += m[k];
                was[k] = true;
            } else {
                break;
            }
        }

        ans = max(ans, cnt);

    }

    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <set>
#include <queue>

using namespace std;

void fastio()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define fo(i, n) for (int i = 0; i < n; ++i)
#define foback(i, n) for (int i = n - 1; i >= 0; --i)
#define fo1(i, n) for (ll i = 1; i <= n; ++i)

typedef long long ll;

void one();

int main()
{
    fastio();

    int t = 1;
    // cin >> t;
    fo(i, t)
    {
        // cout << "Case #" << (i+1) << ": ";
        one();
    }

    return 0;
}

void one()
{
    int n; cin>>n;
    int x; cin>>x;

    vector<int> t(n),h(n),m(n);

    fo(i,n) {
        cin>>t[i]>>h[i]>>m[i];
    }

    int ans=0;
    fo(jstart, 2) {
        int xx = x;

        vector<bool> was(n);
    int cnt=0;
        int cur[2] = {0,0};

        fo(i,n) {
            int type = (jstart + i) % 2;

            int k = -1;
            fo(j,n) {
                if (!was[j] && h[j]<=xx && t[j]==type) {
                    if (k<0 || m[k]<m[j]) {
                        k=j;
                    }
                }
            }

            if (k>=0) {
                cnt++;
                xx += m[k];
                was[k]=true;
            }
        }

        ans = max(ans, cnt);

    }

    cout << ans << endl;
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define tihs if(fopen("input.txt","r"))freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);

#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
inline ll addmod(ll a,ll b){a=a%mod+b%mod;if(a>mod)a%=mod;return a;}
inline ll submod(ll a,ll b){a=a%mod-b%mod;if(a<0)a+=mod;return a;}
inline ll mulmod(ll a,ll b){return (a%mod * b%mod)%mod;}

int dx[]={1,1,0,-1,-1,-1, 0, 1};
int dy[]={0,1,1, 1, 0,-1,-1,-1};
inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }

// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
//   if(n<r)return 0;
//   ll ans=(f[n]%mod * iv[r]%mod)%mod;
//   ans=(ans%mod * iv[n-r]%mod)%mod;
//   return ans%mod;
// }
// void init(){
//   f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

////****************************************************************************************************************************************************************************************************************////

 int main(){
     tihs;
     int n,x;
     cin>>n>>x;
     multiset<pair<int,int>>zero;
     multiset<pair<int,int>>one;
     for(int i=0;i<n;i++){
         int t,h,m;
         cin>>t>>h>>m;
         if(t==0)zero.insert(mp(h,m));
         else one.insert(mp(h,m));
     }

     int jump=x;
     int prev=-1;
     int ans=0;
     int cc=0;
     while(true){
        multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
       if(prev!=0) {for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
       }
     if(prev!=1)
     {for(auto x:one){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }
     }

        if(curr.size()==0)break;
        ans++;
        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one.erase(one.find(extra));
        else zero.erase(zero.find(extra));
        prev=phla.se.se;
        cc++;
     }
      cout<<ans<<endl;

      }


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define tihs if(fopen("input.txt","r"))freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);

#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
inline ll addmod(ll a,ll b){a=a%mod+b%mod;if(a>mod)a%=mod;return a;}
inline ll submod(ll a,ll b){a=a%mod-b%mod;if(a<0)a+=mod;return a;}
inline ll mulmod(ll a,ll b){return (a%mod * b%mod)%mod;}

int dx[]={1,1,0,-1,-1,-1, 0, 1};
int dy[]={0,1,1, 1, 0,-1,-1,-1};
inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }

// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
//   if(n<r)return 0;
//   ll ans=(f[n]%mod * iv[r]%mod)%mod;
//   ans=(ans%mod * iv[n-r]%mod)%mod;
//   return ans%mod;
// }
// void init(){
//   f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

////****************************************************************************************************************************************************************************************************************////

 int main(){
     tihs;
     int n,x;
     cin>>n>>x;
     multiset<pair<int,int>>zero;
     multiset<pair<int,int>>one;
     for(int i=0;i<n;i++){
         int t,h,m;
         cin>>t>>h>>m;
         if(t==0)zero.insert(mp(h,m));
         else one.insert(mp(h,m));
     }

     multiset<pair<int,int>>zero1=zero;
     multiset<pair<int,int>>one1=one;

     int jump=x;
     int prev=-1;

     int ans1=0;
     while(true){
          multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
        if(prev==-1){

           for(auto x:one1){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }

        }

        }
       else if(prev==1) {
           for(auto x:zero1){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
       }
    else  if(prev==0)
     {
         for(auto x:one1){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }
     }

        if(curr.size()==0)break;

        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one1.erase(one1.find(extra));
        else zero1.erase(zero1.find(extra));
        prev=phla.se.se;
        ans1++;

     }
     int ans2=0;
     jump=x;
     prev=-1;
     while(true){
        multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
        if(prev==-1){

          for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }

        }
      else if(prev==1) {
          for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
      }
    else  if(prev==0)
     {for(auto x:one){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }
     }

        if(curr.size()==0)break;

        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one.erase(one.find(extra));
        else zero.erase(zero.find(extra));
        prev=phla.se.se;
        ans2++;
     }
      cout<<MAX2(ans2,ans1)<<endl;

      }


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define tihs if(fopen("input.txt","r"))freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);

#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
inline ll addmod(ll a,ll b){a=a%mod+b%mod;if(a>mod)a%=mod;return a;}
inline ll submod(ll a,ll b){a=a%mod-b%mod;if(a<0)a+=mod;return a;}
inline ll mulmod(ll a,ll b){return (a%mod * b%mod)%mod;}

int dx[]={1,1,0,-1,-1,-1, 0, 1};
int dy[]={0,1,1, 1, 0,-1,-1,-1};
inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }

// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
//   if(n<r)return 0;
//   ll ans=(f[n]%mod * iv[r]%mod)%mod;
//   ans=(ans%mod * iv[n-r]%mod)%mod;
//   return ans%mod;
// }
// void init(){
//   f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

////****************************************************************************************************************************************************************************************************************////

 int main(){
     tihs;
     int n,x;
     cin>>n>>x;
     multiset<pair<int,int>>zero;
     multiset<pair<int,int>>one;
     for(int i=0;i<n;i++){
         int t,h,m;
         cin>>t>>h>>m;
         if(t==0)zero.insert(mp(h,m));
         else one.insert(mp(h,m));
     }

     int jump=x;
     int prev=-1;
     int ans=0;
     while(true){
        multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
        for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
        for(auto x:one){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }

        if(curr.size()==0)break;
        ans++;
        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one.erase(one.find(extra));
        else zero.erase(zero.find(extra));
        prev=phla.se.se;
     }
      cout<<ans<<endl;

      }


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define tihs if(fopen("input.txt","r"))freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);

#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
inline ll addmod(ll a,ll b){a=a%mod+b%mod;if(a>mod)a%=mod;return a;}
inline ll submod(ll a,ll b){a=a%mod-b%mod;if(a<0)a+=mod;return a;}
inline ll mulmod(ll a,ll b){return (a%mod * b%mod)%mod;}

int dx[]={1,1,0,-1,-1,-1, 0, 1};
int dy[]={0,1,1, 1, 0,-1,-1,-1};
inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }

// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
//   if(n<r)return 0;
//   ll ans=(f[n]%mod * iv[r]%mod)%mod;
//   ans=(ans%mod * iv[n-r]%mod)%mod;
//   return ans%mod;
// }
// void init(){
//   f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

////****************************************************************************************************************************************************************************************************************////

 int main(){
     tihs;
     int n,x;
     cin>>n>>x;
     multiset<pair<int,int>>zero;
     multiset<pair<int,int>>one;
     for(int i=0;i<n;i++){
         int t,h,m;
         cin>>t>>h>>m;
         if(t==0)zero.insert(mp(h,m));
         else one.insert(mp(h,m));
     }

     multiset<pair<int,int>>zero1=zero;
     multiset<pair<int,int>>one1=one;

     int jump=x;
     int prev=-1;
     int ans=0;
     int cc=0;
     int ans1=0;
     while(true){
          multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
        if(prev==-1){

           for(auto x:one1){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }

        }

        }
       else if(prev==1) {
           for(auto x:zero1){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
       }
    else  if(prev==0)
     {for(auto x:one1){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }
     }

        if(curr.size()==0)break;
        ans++;
        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one1.erase(one1.find(extra));
        else zero1.erase(zero1.find(extra));
        prev=phla.se.se;
        cc++;
        ans1++;

     }
     int ans2=0;
     while(true){
        multiset<pair<int,pair<int,int>>,greater<pair<int,pair<int,int>>>>curr;
        if(prev==-1){

           for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }

        }
       if(prev==1) {
           for(auto x:zero){
             if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,0)));
             }
        }
       }
     if(prev==0)
     {for(auto x:one){
            if(x.fi <=jump){
                 curr.insert(mp(x.se,mp(x.fi,1)));
             }
        }
     }

        if(curr.size()==0)break;
        ans++;
        pair<int,pair<int,int>>phla;
        for(auto x:curr){
            if(x.se.se!=prev)phla=x;break;
        }
        jump+=phla.fi;
        pair<int,int>extra= mp(phla.se.fi,phla.fi);
        if(phla.se.se==1)one.erase(one.find(extra));
        else zero.erase(zero.find(extra));
        prev=phla.se.se;
        cc++;
        ans2++;
     }
      cout<<MAX2(ans,ans1)<<endl;

      }


#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> //priority_queue<int, vector<int>, greater<int> > q;
#include <map>
#include <set> //multiset set<int,greater<int>>大到小
#include <vector>// vector<int>().swap(v);清空释放内存
#include <stack>
#include <cmath> // auto &Name : STLName  Name.
#include <utility>
#include <sstream>
#include <string>//__builtin_popcount(ans);//获取某个数二进制位1的个数
#define mod 1000000007
#define mod9 998244353
#define ld long double
#define ull unsigned long long
#define db double
#define ll long long
const db eps=1e-7;
//const db pi=acos(-1);
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
#define rep(i,be,en) for (int i=be;i<=en;i++)
#define per(i,be,en) for (int i=en;i>=be;i--)
using namespace std;
const int N=3e3+7;

int n,hi,fl=0,ans=0;
struct node{
    int t,h,m;
};
bool cmp(node l,node r){
    if(l.m==r.m) return l.h<r.h;
    return l.m>r.m;
}
vector<node> v,vv;
int main(){
    scanf("%d%d",&n,&hi);
    rep(i,1,n){
        int t,h,mm;
        scanf("%d%d%d",&t,&h,&mm);
        v.push_back({t,h,mm});
        vv.push_back({t,h,mm});
    }
    sort(v.begin(),v.end(),cmp);
    sort(vv.begin(),vv.end(),cmp);
    int high=hi,cnt=0;
    fl=0;
    for(int i=0;i<v.size();i++){
        if(v[i].t==fl&&v[i].h<=high){
            high+=v[i].m;
//            printf("high1=%d\n",high);
            cnt++;
            fl^=1;
            v.erase(v.begin()+i);
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].t);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].h);
//            printf("\n");
//            for(int j=0;j<v.size();j++) printf("%d ",v[j].m);
//            printf("\n");
            i=-1;
        }
    }
    ans=max(ans,cnt);
    cnt=0,high=hi,fl=1;
    for(int i=0;i<vv.size();i++){
        if(vv[i].t==fl&&vv[i].h<=high){
            high+=vv[i].m;
//            printf("high2=%d\n",high);
            cnt++;
            fl^=1;
            vv.erase(vv.begin()+i);
            i=-1;
        }
    }
    ans=max(ans,cnt);
    printf("%d\n",ans);
    return 0;
}