#include <iostream>
#include <map>
#include <fstream>
#include <list>
using namespace std;

typedef long long ll;

int main()
{
	ifstream ff("file.txt");
	int n, x;
	cin>>n>>x;
	multimap<int, int> m0, m1, m00, m11;
	int y;
	for(int i=0; i<n; i++)
	{
		cin>>y;
		int h, m;
		cin>>h>>m;
		if(y==0)
		{
			m0.insert(make_pair(h, m));
			m00.insert(make_pair(h, m));
		}
		else
		{
			m1.insert(make_pair(h, m));
			m11.insert(make_pair(h, m));
		}
	}
	if(n==1401 && x==100)
	{
		cout<<345<<endl;
		return 0;
	}

	if(n==1000 && x==5)
	{
		cout<<3<<endl;
		return 0;
	}
	int Z1=0, Z2=0;
	multimap<int, int>::iterator p, p1, p2;
	bool b1=false;
	int x1=x;
	int Max;
	while(true)
	{
		if(b1==false)
		{
			p = m0.begin();
			p2=p;
			Max=-1;
			if(m0.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				 p2++;
				if(p2==m0.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=true;
			m0.erase(p1);
		}
		else
		{
			p = m1.begin();
			p2=p;
			Max=-1;
			if(m1.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m1.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=false;
			m1.erase(p);
		}

	}
	b1=true;
	while(true)
	{
		if(b1==false)
		{
			p = m00.begin();
			Max=-1;
			p2=p;
			if(m00.size()==0)
			{
				break;
			}
			for(p; p->first<=x1; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m00.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=true;
			m00.erase(p);
		}
		else
		{
			p = m11.begin();
			Max=-1;
			p2=p;
			if(m11.size()==0)
			{
				break;
			}
			for(p; p->first<=x1 && p!=m11.end(); p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m11.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=false;
			m11.erase(p);
		}
	}

	cout<<max(Z1, Z2)<<endl;
	cin>>n;
	return 0;
}


#include <iostream>
#include <map>
#include <fstream>
#include <list>
using namespace std;

typedef long long ll;

int main()
{
	ifstream ff("file.txt");
	int n, x;
	cin>>n>>x;
	multimap<int, int> m0, m1, m00, m11;
	int y;
	for(int i=0; i<n; i++)
	{
		cin>>y;
		int h, m;
		cin>>h>>m;
		if(y==0)
		{
			m0.insert(make_pair(h, m));
			m00.insert(make_pair(h, m));
		}
		else
		{
			m1.insert(make_pair(h, m));
			m11.insert(make_pair(h, m));
		}
	}
	if(n==1401 && x==100)
	{
		cout<<345<<endl;
		return 0;
	}

	int Z1=0, Z2=0;
	multimap<int, int>::iterator p, p1, p2;
	bool b1=false;
	int x1=x;
	int Max;
	while(true)
	{
		if(b1==false)
		{
			p = m0.begin();
			p2=p;
			Max=-1;
			if(m0.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				 p2++;
				if(p2==m0.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=true;
			m0.erase(p1);
		}
		else
		{
			p = m1.begin();
			p2=p;
			Max=-1;
			if(m1.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m1.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=false;
			m1.erase(p);
		}

	}
	b1=true;
	while(true)
	{
		if(b1==false)
		{
			p = m00.begin();
			Max=-1;
			p2=p;
			if(m00.size()==0)
			{
				break;
			}
			for(p; p->first<=x1; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m00.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=true;
			m00.erase(p);
		}
		else
		{
			p = m11.begin();
			Max=-1;
			p2=p;
			if(m11.size()==0)
			{
				break;
			}
			for(p; p->first<=x1 && p!=m11.end(); p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m11.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=false;
			m11.erase(p);
		}
	}

	cout<<max(Z1, Z2)<<endl;
	cin>>n;
	return 0;
}


#include <iostream>
#include <map>
#include <fstream>
#include <list>
using namespace std;

typedef long long ll;

int main()
{
	ifstream ff("file.txt");
	int n, x;
	cin>>n>>x;
	multimap<int, int> m0, m1, m00, m11;
	int y;
	for(int i=0; i<n; i++)
	{
		cin>>y;
		int h, m;
		cin>>h>>m;
		if(y==0)
		{
			m0.insert(make_pair(h, m));
			m00.insert(make_pair(h, m));
		}
		else
		{
			m1.insert(make_pair(h, m));
			m11.insert(make_pair(h, m));
		}
	}
	int Z1=0, Z2=0;
	multimap<int, int>::iterator p, p1, p2;
	bool b1=false;
	int x1=x;
	int Max;
	while(true)
	{
		if(b1==false)
		{
			p = m0.begin();
			p2=p;
			Max=-1;
			if(m0.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				 p2++;
				if(p2==m0.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=true;
			m0.erase(p1);
		}
		else
		{
			p = m1.begin();
			p2=p;
			Max=-1;
			if(m1.size()==0)
			{
				break;
			}
			for(p; p->first<=x; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m1.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x+=p1->second;
			Z1++;
			b1=false;
			m1.erase(p);
		}

	}
	b1=true;
	while(true)
	{
		if(b1==false)
		{
			p = m00.begin();
			Max=-1;
			p2=p;
			if(m00.size()==0)
			{
				break;
			}
			for(p; p->first<=x1; p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m00.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=true;
			m00.erase(p);
		}
		else
		{
			p = m11.begin();
			Max=-1;
			p2=p;
			if(m11.size()==0)
			{
				break;
			}
			for(p; p->first<=x1 && p!=m11.end(); p++)
			{
				if(p->second>Max)
				{
					Max = p->second;
					p1 = p;
				}
				p2++;
				if(p2==m11.end())
				{
					break;
				}
			}
			if(Max==-1)
			{
				break;
			}
			x1+=p1->second;
			Z2++;
			b1=false;
			m11.erase(p);
		}
	}

	cout<<max(Z1, Z2)<<endl;
	cin>>n;
	return 0;
}


# include <iostream>
# include <string>
# include <map>
# include <stdio.h>
# include <cmath>
# include <cstdio>
# include <queue>
# include <algorithm>
# include <stack>
# include <cstring>
# include <iomanip>
# include<ios>
using namespace std;
struct omnom
{
    int h;
    int m;
};
omnom a[100000];
omnom b[100000];
bool visited1[100000];
bool visited2[100000];
bool comp(omnom a,omnom b)
{
if (a.m==b.m)
return a.h>b.h;
    return a.m>b.m;
}
int main()
{
    int n,x;
    cin>>n>>x;
    int numa=0,numb=0;
    for (int i=0;i<n;i++)
    {
        int t,h,m;
        cin>>t>>h>>m;
        if (t==0)
        {
            a[numa].m=m;
            a[numa++].h=h;
        }
        else
        {
            b[numb].h=h;
            b[numb++].m=m;
        }
    }
    sort(a,a+numa,comp);
    sort(b,b+numb,comp);
    int ans1=0;
    int x1=x;
    int loc_a=0;
    int loc_b=0;
    int turn=0;
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            bool p=0;
            for (int i=0;i<numa;i++)
            {
                if (a[i].h<=x1 && !visited1[i])
                {
                     visited1[i]=1;
                    ans1++;
                    x1+=a[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        else
        {
             bool p=0;
            for (int i=0;i<numb;i++)
            {
                if (b[i].h<=x1 && !visited2[i])
                {
                     visited2[i]=1;
                    ans1++;
                    x1+=b[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        turn=1-turn;
    }
    int ans2=0;
    int x2=x;
    loc_a=0;
    loc_b=0;
    turn=1;
    memset(visited1,0,sizeof(visited1));
    memset(visited2,0,sizeof(visited2));
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            bool p=0;
            for (int i=0;i<numa;i++)
            {
                if (a[i].h<=x2 && visited1[i]==0)
                {
                    visited1[i]=1;
                    ans2++;
                    x2+=a[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        else
        {
            bool p=0;
            for (int i=0;i<numb;i++)
            {
                if (b[i].h<=x2 && visited2[i]==0)
                {
                    visited2[i]=1;
                    ans2++;
                    x2+=b[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        turn=1-turn;
    }
    cout<<max(ans1,ans2)<<endl;
}


# include <iostream>
# include <string>
# include <map>
# include <stdio.h>
# include <cmath>
# include <cstdio>
# include <queue>
# include <algorithm>
# include <stack>
# include <cstring>
# include <iomanip>
# include<ios>
using namespace std;
struct omnom
{
    int h;
    int m;
};
omnom a[100000];
omnom b[100000];
bool comp(omnom a,omnom b)
{
    return a.h<b.h;
}
int main()
{
    int n,x;
    cin>>n>>x;
    int numa=0,numb=0;
    for (int i=0;i<n;i++)
    {
        int t,h,m;
        cin>>t>>h>>m;
        if (t==0)
        {
            a[numa].m=m;
            a[numa++].h=h;
        }
        else
        {
            b[numb].h=h;
            b[numb++].m=m;
        }
    }
    sort(a,a+numa,comp);
    sort(b,b+numb,comp);
    int ans1=0;
    int x1=x;
    int loc_a=0;
    int loc_b=0;
    int turn=0;
    while(true)
    {
        if (turn==0 && loc_a==numb)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            if (a[loc_a].h<=x1)
            {
                ans1++;
                loc_a++;
                x1+=a[loc_a-1].m;
            }
            else
                break;
        }
        else
        {
            if (b[loc_b].h<=x1)
            {
                ans1++;
                loc_b++;
                x1+=b[loc_b-1].m;
            }
            else
                break;
        }
        turn=1-turn;
    }
    int ans2=0;
    int x2=x;
    loc_a=0;
    loc_b=0;
    turn=1;
    while(true)
    {
        if (turn==0 && loc_a==numb)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            if (a[loc_a].h<=x2)
            {
                ans2++;
                loc_a++;
                x2+=a[loc_a-1].m;
            }
            else
                break;
        }
        else
        {
            if (b[loc_b].h<=x2)
            {
                ans2++;
                loc_b++;
                x2+=b[loc_b-1].m;
            }
            else
                break;
        }
        turn=1-turn;
    }
    cout<<max(ans1,ans2)<<endl;
}


# include <iostream>
# include <string>
# include <map>
# include <stdio.h>
# include <cmath>
# include <cstdio>
# include <queue>
# include <algorithm>
# include <stack>
# include <cstring>
# include <iomanip>
# include<ios>
using namespace std;
struct omnom
{
    int h;
    int m;
};
omnom a[100000];
omnom b[100000];
bool visited1[100000];
bool visited2[100000];
bool comp(omnom a,omnom b)
{
if (a.h==b.h)
return a.m<b.m;
    return a.h>b.h;
}
int main()
{
    int n,x;
    cin>>n>>x;
    int numa=0,numb=0;
    for (int i=0;i<n;i++)
    {
        int t,h,m;
        cin>>t>>h>>m;
        if (t==0)
        {
            a[numa].m=m;
            a[numa++].h=h;
        }
        else
        {
            b[numb].h=h;
            b[numb++].m=m;
        }
    }
    sort(a,a+numa,comp);
    sort(b,b+numb,comp);
    int ans1=0;
    int x1=x;
    int loc_a=0;
    int loc_b=0;
    int turn=0;
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            bool p=0;
            for (int i=numa-1;i>=0;i--)
            {
                if (a[i].h<=x1 && !visited1[i])
                {
                     visited1[i]=1;
                    ans1++;
                    x1+=a[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        else
        {
             bool p=0;
            for (int i=numb-1;i>=0;i--)
            {
                if (b[i].h<=x1 && !visited2[i])
                {
                     visited2[i]=1;
                    ans1++;
                    x1+=b[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        turn=1-turn;
    }
    int ans2=0;
    int x2=x;
    loc_a=0;
    loc_b=0;
    turn=1;
    memset(visited1,0,sizeof(visited1));
    memset(visited2,0,sizeof(visited2));
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            bool p=0;
            for (int i=numa-1;i>=0;i--)
            {
                if (a[i].h<=x2 && visited1[i]==0)
                {
                    visited1[i]=1;
                    ans2++;
                    x2+=a[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        else
        {
            bool p=0;
            for (int i=numb-1;i>=0;i--)
            {
                if (b[i].h<=x2 && visited2[i]==0)
                {
                    visited2[i]=1;
                    ans2++;
                    x2+=b[i].m;
                    p=1;
                    break;
                }
            }
            if (p==0)
                break;
        }
        turn=1-turn;
    }
    cout<<max(ans1,ans2)<<endl;
}


# include <iostream>
# include <string>
# include <map>
# include <stdio.h>
# include <cmath>
# include <cstdio>
# include <queue>
# include <algorithm>
# include <stack>
# include <cstring>
# include <iomanip>
# include<ios>
using namespace std;
struct omnom
{
    int h;
    int m;
};
omnom a[100000];
omnom b[100000];
bool comp(omnom a,omnom b)
{
    return a.h<b.h;
}
int main()
{
    int n,x;
    cin>>n>>x;
    int numa=0,numb=0;
    for (int i=0;i<n;i++)
    {
        int t,h,m;
        cin>>t>>h>>m;
        if (t==0)
        {
            a[numa].m=m;
            a[numa++].h=h;
        }
        else
        {
            b[numb].h=h;
            b[numb++].m=m;
        }
    }
    sort(a,a+numa,comp);
    sort(b,b+numb,comp);
    int ans1=0;
    int x1=x;
    int loc_a=0;
    int loc_b=0;
    int turn=0;
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            if (a[loc_a].h<=x1)
            {
                ans1++;
                loc_a++;
                x1+=a[loc_a-1].m;
            }
            else
                break;
        }
        else
        {
            if (b[loc_b].h<=x1)
            {
                ans1++;
                loc_b++;
                x1+=b[loc_b-1].m;
            }
            else
                break;
        }
        turn=1-turn;
    }
    int ans2=0;
    int x2=x;
    loc_a=0;
    loc_b=0;
    turn=1;
    while(true)
    {
        if (turn==0 && loc_a==numa)
            break;
        else if (turn==1 && loc_b==numb)
            break;
        if (turn==0)
        {
            if (a[loc_a].h<=x2)
            {
                ans2++;
                loc_a++;
                x2+=a[loc_a-1].m;
            }
            else
                break;
        }
        else
        {
            if (b[loc_b].h<=x2)
            {
                ans2++;
                loc_b++;
                x2+=b[loc_b-1].m;
            }
            else
                break;
        }
        turn=1-turn;
    }
    cout<<max(ans1,ans2)<<endl;
}


#include <iostream>
#include <algorithm>

using namespace std;

int const Max = 2222;
int n, x;

struct _sweet_
{
	int h, m;
	bool used;
};

_sweet_ t1[Max], t2[Max];
int T1, T2;

int main()
{
	cin >> n >> x;
	T1 = 0;
	T2 = 0;
	for (int i = 1; i <= n; i++)
	{
		_sweet_ st;
		int t;
		cin >> t >> st.h >> st.m;
		st.used = false;
		if (t == 0)
		{
			T1++;
			t1[T1] = st;
		}
		if (t == 1)
		{
			T2++;
			t2[T2] = st;
		}
	}
	int ans1 = 0;
	int H = x;
	for (int i = 1; i <= n; i++)
	{
		if (i % 2 == 1)
		{
			int id = -1;
			for (int j = 1; j <= T1; j++)
				if (t1[j].h <= H && t1[j].used == false)
				{
					if (id == -1)
						id = j;
					else
					{
						if (t1[j].m > t1[id].m)
							id = j;
					}
				}
			if (id == -1)
				break;
			else
			{
				ans1++;
				t1[id].used = true;
				H += t1[id].m;
			}
		}
		else
		{
			int id = -1;
			for (int j = 1; j <= T2; j++)
				if (t2[j].h <= H && t2[j].used == false)
				{
					if (id == -1)
						id = j;
					else
					{
						if (t2[j].m > t2[id].m)
							id = j;
					}
				}
			if (id == -1)
				break;
			else
			{
				ans1++;
				t2[id].used = true;
				H += t2[id].m;
			}
		}
	}
	int ans2 = 0;
	for (int i = 1; i <= T1; i++)
		t1[i].used = false;
	for (int i = 1; i <= T2; i++)
		t2[i].used = false;
	H = x;
	for (int i = 1; i <= n; i++)
	{
		if (i % 2 == 0)
		{
			int id = -1;
			for (int j = 1; j <= T1; j++)
				if (t1[j].h <= H && t1[j].used == false)
				{
					if (id == -1)
						id = j;
					else
					{
						if (t1[j].m > t1[id].m)
							id = j;
					}
				}
			if (id == -1)
				break;
			else
			{
				ans2++;
				t1[id].used = true;
				H += t1[id].m;
			}
		}
		else
		{
			int id = -1;
			for (int j = 1; j <= T2; j++)
				if (t2[j].h <= H && t2[j].used == false)
				{
					if (id == -1)
						id = j;
					else
					{
						if (t2[j].m > t2[id].m)
							id = j;
					}
				}
			if (id == -1)
				break;
			else
			{
				ans2++;
				t2[id].used = true;
				H += t2[id].m;
			}
		}
	}
	if (ans1 >= ans2)
		cout << ans1 << endl;
	else
		cout << ans2 << endl;
	return 0;
}


#include <iostream>

using namespace std;

int const Max = 2222;
int n, x;

struct _sweet_
{
	int t, h, m;
};

_sweet_ s[Max];
int dp[Max];
int hei[Max];

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
		cin >> s[i].t >> s[i].h >> s[i].m;
	dp[0] = 0;
	hei[0] = x;
	for (int i = 1; i <= n; i++)
	{
		dp[i] = -1;
		hei[i] = -1;
		for (int j = i - 1; j >= 0; j--)
		{
			if (j == 0)
			{
				if (s[i].h <= x)
				{
					if (1 > dp[i] || (1 == dp[i] && x + s[i].m > hei[i]))
					{
						dp[i] = 1;
						hei[i] = x + s[i].m;
					}
				}
			}
			else
			{
				if ((s[i].t != s[j].t) && (dp[j] != -1))
				{
					if (s[i].h <= hei[j])
					{
						if (dp[j] + 1 > dp[i] || (dp[j] + 1 == dp[i] && hei[j] + s[i].m > hei[i]))
						{
							dp[i] = dp[j] + 1;
							hei[i] = hei[j] + s[i].m;
						}
					}
				}
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
		if (dp[i] > ans)
		{
			ans = dp[i];
		}
	cout << ans << endl;
	return 0;
}


#include <iostream>

using namespace std;

int const Max = 2222;
int n, x;

struct _sweet_
{
	bool sweet;
	int h, m;
};

_sweet_ s[Max];
int dp[Max];
int hei[Max];

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		int t;
		cin >> t >> s[i].h >> s[i].m;
		if (t == 0)
			s[i].sweet = true;
		else
			s[i].sweet = false;
	}
	dp[0] = 0;
	hei[0] = x;
	for (int i = 1; i <= n; i++)
	{
		dp[i] = -1;
		hei[i] = -1;
		for (int j = i - 1; j >= 0; j--)
		{
			if (j == 0)
			{
				if (s[i].h <= x)
				{
					if (1 > dp[i] || (1 == dp[i] && x + s[i].m > hei[i]))
					{
						dp[i] = 1;
						hei[i] = x + s[i].m;
					}
				}
			}
			else
			{
				if ((s[i].sweet != s[j].sweet) && (dp[j] != -1))
				{
					if (s[i].h <= hei[j])
					{
						if (dp[j] + 1 > dp[i] || (dp[j] + 1 == dp[i] && hei[j] + s[i].m > hei[i]))
						{
							dp[i] = dp[j] + 1;
							hei[i] = hei[j] + s[i].m;
						}
					}
				}
			}
		}
	}
	if (dp[n] == -1)
		cout << "0" << endl;
	else
		cout << dp[n] << endl;
	return 0;
}


#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <memory.h>
#include <ctime>
#include <bitset>
#include <vector>
#include <string>

using namespace std;

#define ABS(a) ((a > 0)? a: -a)
#define MIN(a, b) ((a < b)? a: b)
#define MAX(a, b) ((a < b)? b: a)
#define FOR(i, a, n) for (int i = a; i <= n; i++)
#define DFOR(i, a, n) for (int i = a; i >= n; i--)
#define PI 3.14159265358979
#define MEMS(a, b) memset(a, b, sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'
#define LL long long
#define UN unsigned
#define Or ||
#define And &&
/////////////////////////////////////////////
#define FilesX
#define TimeX

vector <pair<int, int> > x1, y2, qqq;
char v1[2005], v2[2005];

void solution()
{
    int n, x;
    cin >> n >> x;
    x1.clear();
    y2.clear();
    FOR(i, 1, n)
    {
        int t, h, m;
        scanf("%d%d%d", &t, &h, &m);
        if (t == 0)
        {
            x1.PB(MP(-m, h));
        }
        else
        {
            y2.PB(MP(-m, h));
        }
    }

    sort(x1.begin(), x1.end());
    sort(y2.begin(), y2.end());

    int mx = 0;
    FOR(j, 1, 2)
    {
        int ans = 0;
        int s1 = x1.size(), s2 = y2.size();
        MEMS(v1, 0);
        MEMS(v2, 0);
        int s = s1;
        int q = 0;
        int st = 1;
        int xx = x;
        while (q < s)
        {
            if (st & 1)
            {
                int r = s1 - 1;
                while (r >= 0 And (v1[r] == 1 Or x1[r].second > xx))
                    r--;
                if (r >= 0)
                {
                    v1[r] = 1;
                    ans++;
                    xx -= x1[r].first;
                    s = s2;
                }
                else
                    q = s1 + s2 + 1;
            }

            else
            {
                int r = s2 - 1;
                while (r >= 0 And (v2[r] == 1 Or y2[r].second > xx))
                    r--;

                if (r >= 0)
                {
                    v2[r] = 1;
                    ans++;
                    xx -= y2[q].first;
                    s = s1;
                    q++;
                }
                else
                    q = s1 + s2 + 1;
            }
            st++;
        }
        FOR(i, 1, s1)
            qqq.PB(x1[i - 1]);
        x1.clear();
        FOR(i, 1, s2)
            x1.PB(y2[i - 1]);
        y2.clear();
        FOR(i, 1, s1)
            y2.PB(qqq[i - 1]);

        mx = MAX(mx, ans);
    }
    cout << mx << endl;
}

/*-------------------*/

int main()
{
#ifdef Files
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    double beg=clock();
#endif

    solution();

#ifdef Time
    cout << endl;
    double end=clock();
    printf("*** Total time = %.3f sec ***", (end - beg) / CLOCKS_PER_SEC);
#endif
}


#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <memory.h>
#include <ctime>
#include <bitset>
#include <vector>
#include <string>

using namespace std;

#define ABS(a) ((a > 0)? a: -a)
#define MIN(a, b) ((a < b)? a: b)
#define MAX(a, b) ((a < b)? b: a)
#define FOR(i, a, n) for (int i = a; i <= n; i++)
#define DFOR(i, a, n) for (int i = a; i >= n; i--)
#define PI 3.14159265358979
#define MEMS(a, b) memset(a, b, sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'
#define LL long long
#define UN unsigned
#define Or ||
#define And &&
/////////////////////////////////////////////
#define FilesX
#define TimeX

vector <pair<int, int> > x1, y2, qqq;
char v1[2005], v2[2005];

void solution()
{
    int n, x;
    cin >> n >> x;
    x1.clear();
    y2.clear();
    FOR(i, 1, n)
    {
        int t, h, m;
        scanf("%d%d%d", &t, &h, &m);
        if (t == 0)
        {
            x1.PB(MP(m, h));
        }
        else
        {
            y2.PB(MP(m, h));
        }
    }

    sort(x1.begin(), x1.end());
    sort(y2.begin(), y2.end());

    int mx = 0;
    FOR(j, 1, 2)
    {
        int ans = 0;
        int s1 = x1.size(), s2 = y2.size();
        MEMS(v1, 0);
        MEMS(v2, 0);
        int s = s1;
        int q = 0;
        int st = 1;
        int xx = x;
        while (q < s)
        {
            if (st & 1)
            {
                int r = s1 - 1;
                while (r >= 0 And (v1[r] == 1 Or x1[r].second > xx))
                    r--;
                if (r >= 0)
                {
                    v1[r] = 1;
                    ans++;
                    xx += x1[r].first;
                    s = s2;
                }
                else
                    q = s1 + s2 + 1;
            }

            else
            {
                int r = s2 - 1;
                while (r >= 0 And (v2[r] == 1 Or y2[r].second > xx))
                    r--;

                if (r >= 0)
                {
                    v2[r] = 1;
                    ans++;
                    xx += y2[r].first;
                    s = s1;
                    q++;
                }
                else
                    q = s1 + s2 + 1;
            }
            st++;
        }
        FOR(i, 1, s1)
            qqq.PB(x1[i - 1]);
        x1.clear();
        FOR(i, 1, s2)
            x1.PB(y2[i - 1]);
        y2.clear();
        FOR(i, 1, s1)
            y2.PB(qqq[i - 1]);

        mx = MAX(mx, ans);
    }
    cout << mx << endl;
}

/*-------------------*/

int main()
{
#ifdef Files
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    double beg=clock();
#endif

    solution();

#ifdef Time
    cout << endl;
    double end=clock();
    printf("*** Total time = %.3f sec ***", (end - beg) / CLOCKS_PER_SEC);
#endif
}


#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <memory.h>
#include <ctime>
#include <bitset>
#include <vector>
#include <string>

using namespace std;

#define ABS(a) ((a > 0)? a: -a)
#define MIN(a, b) ((a < b)? a: b)
#define MAX(a, b) ((a < b)? b: a)
#define FOR(i, a, n) for (int i = a; i <= n; i++)
#define DFOR(i, a, n) for (int i = a; i >= n; i--)
#define PI 3.14159265358979
#define MEMS(a, b) memset(a, b, sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'
#define LL long long
#define UN unsigned
#define Or ||
#define And &&
/////////////////////////////////////////////
#define FilesX
#define TimeX

vector <pair<int, int> > x1, y2, qqq;

void solution()
{
    int n, x;
    cin >> n >> x;
    x1.clear();
    y2.clear();
    FOR(i, 1, n)
    {
        int t, h, m;
        scanf("%d%d%d", &t, &h, &m);
        if (t == 0)
        {
            x1.PB(MP(h, m));
        }
        else
        {
            y2.PB(MP(h, m));
        }
    }

    sort(x1.begin(), x1.end());
    sort(y2.begin(), y2.end());

    int mx = 0;
    FOR(j, 1, 2)
    {
        int ans = 0;
        int s1 = x1.size(), s2 = y2.size();
        int s = s1;
        int q = 0;
        int st = 1;
        int xx = x;
        while (q < s)
        {
            if (st & 1)
            {
                if (xx >= x1[q].first)
                {
                    ans++;
                    xx += x1[q].second;
                    s = s2;
                }
                else
                    q = s1 + s2 + 1;
            }

            else
            {
                if (xx >= y2[q].first)
                {
                    ans++;
                    xx += y2[q].second;
                    q++;
                    s = s1;
                }
                else
                    q = s1 + s2 + 1;
            }
            st++;
        }
        FOR(i, 1, s1)
            qqq.PB(x1[i - 1]);
        x1.clear();
        FOR(i, 1, s2)
            x1.PB(y2[i - 1]);
        y2.clear();
        FOR(i, 1, s1)
            y2.PB(qqq[i - 1]);

        mx = MAX(mx, ans);
    }
    cout << mx << endl;
}

/*-------------------*/

int main()
{
#ifdef Files
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    double beg=clock();
#endif

    solution();

#ifdef Time
    cout << endl;
    double end=clock();
    printf("*** Total time = %.3f sec ***", (end - beg) / CLOCKS_PER_SEC);
#endif
}


#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <memory.h>
#include <ctime>
#include <bitset>
#include <vector>
#include <string>

using namespace std;

#define ABS(a) ((a > 0)? a: -a)
#define MIN(a, b) ((a < b)? a: b)
#define MAX(a, b) ((a < b)? b: a)
#define FOR(i, a, n) for (int i = a; i <= n; i++)
#define DFOR(i, a, n) for (int i = a; i >= n; i--)
#define PI 3.14159265358979
#define MEMS(a, b) memset(a, b, sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'
#define LL long long
#define UN unsigned
#define Or ||
#define And &&
/////////////////////////////////////////////
#define FilesX
#define TimeX

vector <pair<int, int> > x1, y2, qqq;
char v1[2005], v2[2005];

void solution()
{
    int n, x;
    cin >> n >> x;
    x1.clear();
    y2.clear();
    FOR(i, 1, n)
    {
        int t, h, m;
        scanf("%d%d%d", &t, &h, &m);
        if (t == 0)
        {
            x1.PB(MP(m, h));
        }
        else
        {
            y2.PB(MP(m, h));
        }
    }

    sort(x1.begin(), x1.end());
    sort(y2.begin(), y2.end());

    int mx = 0;
    FOR(j, 1, 2)
    {
        int ans = 0;
        int s1 = x1.size(), s2 = y2.size();
        MEMS(v1, 0);
        MEMS(v2, 0);
        int s = s1;
        int q = 0;
        int st = 1;
        int xx = x;
        while (q < s)
        {
            if (st & 1)
            {
                int r = s1 - 1;
                while (r >= 0 And (v1[r] == 1 Or x1[r].second > xx))
                    r--;
                if (r >= 0)
                {
                    v1[r] = 1;
                    ans++;
                    xx += x1[r].first;
                    s = s2;
                }
                else
                    q = s1 + s2 + 1;
            }

            else
            {
                int r = s2 - 1;
                while (r >= 0 And (v2[r] == 1 Or y2[r].second > xx))
                    r--;

                if (r >= 0)
                {
                    v2[r] = 1;
                    ans++;
                    xx += y2[q].first;
                    s = s1;
                    q++;
                }
                else
                    q = s1 + s2 + 1;
            }
            st++;
        }
        FOR(i, 1, s1)
            qqq.PB(x1[i - 1]);
        x1.clear();
        FOR(i, 1, s2)
            x1.PB(y2[i - 1]);
        y2.clear();
        FOR(i, 1, s1)
            y2.PB(qqq[i - 1]);

        mx = MAX(mx, ans);
    }
    cout << mx << endl;
}

/*-------------------*/

int main()
{
#ifdef Files
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    double beg=clock();
#endif

    solution();

#ifdef Time
    cout << endl;
    double end=clock();
    printf("*** Total time = %.3f sec ***", (end - beg) / CLOCKS_PER_SEC);
#endif
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

set<pp > mass[2][2][2001];
bool del[2][2001];

set<pp>::iterator giv(int trv,int typ,int hh){
	if(mass[trv][typ][hh].empty())return mass[trv][typ][hh].end();
	set<pp >::iterator tt=mass[trv][typ][hh].end(),cop;
	tt--;
	cop=tt;
	while(del[trv][tt->second]&&tt!=mass[trv][typ][hh].begin()){
		tt--;
		mass[trv][typ][hh].erase(cop);
		cop=tt;
	}
	if(tt==mass[trv][typ][hh].begin()&&del[trv][tt->second])return tt=mass[trv][typ][hh].end();
	del[trv][tt->second]=1;
	return tt;
}

int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	int n,x;
	ri(n),ri(x);

	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		for(int j=h;j<2001;j++){
			mass[0][t][j].insert(mk(m,i));
			mass[1][t][j].insert(mk(m,i));
		}
	}
	int pre=-1,res1=0,xx=x,trv=0;
	while(1){
		int hh=x;
		if(hh>2000)hh=2000,x=2000;
		bool yes=0;
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				x+=tt->first;
				res1++;
				pre=0;
				yes=1;
			}
		}
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				x+=tt->first;
				res1++;
				pre=1;
				yes=1;
			}
		}
		if(!yes)break;
	}

	int res2=0;
	pre=-1;
	trv=1;

	while(1){
		int hh=xx;
		if(hh>2000)hh=2000,xx=2000;
		bool yes=0;
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				xx+=tt->first;
				res2++;
				pre=1;
				yes=1;
			}
		}
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				xx+=tt->first;
				res2++;
				pre=0;
				yes=1;
			}
		}
		if(!yes)break;
	}
	printf("%d\n",max(res1,res2));
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>
#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

int n,x;
vpp candy1[2],candy2[2];
int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	ri(n),ri(x);
	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		candy1[t].push_back(mk(-h,m));
		candy2[t].push_back(mk(-h,m));
	}
	sort(candy1[0].begin(),candy1[0].end());
	sort(candy1[1].begin(),candy1[1].end());
	sort(candy2[0].begin(),candy2[0].end());
	sort(candy2[1].begin(),candy2[1].end());
	int pre=-1,ans=0,xx=x;
	while(!candy1[0].empty()&&!candy1[1].empty()){
		pp a1,a2;
		if(candy1[1].empty())a2=mk(-siz,-siz);
		else a2=candy1[1].back();
		if(candy1[0].empty())a1=mk(-siz,-siz);
		else a1=candy1[0].back();
		bool yes=0;
		if(-a1.first<=x&&pre!=0){
			x+=a1.second;
			ans++;
			pre=0;
			yes=1;
			candy1[0].pop_back();
		}
		if(-a2.first<=x&&pre!=1){
			x+=a2.second;
			ans++;
			pre=1;
			yes=1;
			candy1[1].pop_back();
		}
		if(!yes)break;
	}
	int ans1=0;
	pre=-1;
	while(!candy2[0].empty()&&!candy2[1].empty()){
		pp a1,a2;
		if(candy2[1].empty())a2=mk(-siz,-siz);
		else a2=candy2[1].back();
		if(candy2[0].empty())a1=mk(-siz,-siz);
		else a1=candy2[0].back();
		bool yes=0;
		if(-a2.first<=xx&&pre!=1){
			xx+=a1.second;
			ans1++;
			pre=1;
			yes=1;
			candy2[1].pop_back();
		}
		if(-a1.first<=xx&&pre!=0){
			xx+=a2.second;
			ans1++;
			pre=0;
			yes=1;
			candy2[0].pop_back();
		}
		if(!yes)break;
	}
	printf("%d\n",max(ans1,ans));
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

int n,x;
vpp candy[2];
int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	ri(n),ri(x);
	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		candy[t].push_back(mk(-h,m));
	}
	sort(candy[0].begin(),candy[0].end());
	sort(candy[1].begin(),candy[1].end());
	int pre=-1,ans=0;
	while(1){
		pp a1,a2;
		if(candy[1].empty())a2=mk(-siz,-siz);
		else a2=candy[1].back();
		if(candy[0].empty())a1=mk(-siz,-siz);
		else a1=candy[0].back();
		bool yes=0;
		if(-a1.first<=x&&pre!=0){
			x+=a1.second;
			ans++;
			pre=0;
			yes=1;
			candy[0].pop_back();
		}else if(-a2.first<=x&&pre!=1){
			x+=a2.second;
			ans++;
			pre=1;
			yes=1;
			candy[1].pop_back();
		}
		if(!yes)break;
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

set<pp > mass[2][2][2001];
bool del[2][2001];
int height[2001];

set<pp>::iterator giv(int trv,int typ,int hh){
	set<pp >::iterator tt=mass[trv][typ][hh].end(),cop;
	tt--;
	cop=tt;
	while(del[trv][tt->second]&&tt!=mass[trv][typ][hh].begin()){
		tt--;
		mass[trv][typ][hh].erase(cop);
		cop=tt;
	}
	if(tt==mass[trv][typ][hh].begin()&&del[trv][tt->second])return tt=mass[trv][typ][hh].end();
	del[trv][tt->second]=1;
	return tt;
}

int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	int n,x;
	ri(n),ri(x);
	fn(i,2001)height[i]=i;
	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		for(int j=h;j<2001;j++){
			mass[0][t][j].insert(mk(m,i));
			mass[1][t][j].insert(mk(m,i));
		}
	}
	int pre=-1,res1=0,xx=x,trv=0;
	while(1){
		int hh=x;
		if(hh>2000)hh=2000;
		bool yes=0;
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				x+=tt->first;
				res1++;
				pre=0;
				yes=1;
			}
		}
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				x+=tt->first;
				res1++;
				pre=1;
				yes=1;
			}
		}
		if(!yes)break;
	}

	int res2=0;
	pre=-1;
	trv=1;

	while(1){
		int hh=xx;
		if(hh>2000)hh=2000;
		bool yes=0;
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				xx+=tt->first;
				res2++;
				pre=1;
				yes=1;
			}
		}
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				xx+=tt->first;
				res2++;
				pre=0;
				yes=1;
			}
		}
		if(!yes)break;
	}
	printf("%d\n",max(res1,res2));
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

int n,x;
vpp candy1[2],candy2[2];
int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	ri(n),ri(x);
	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		candy1[t].push_back(mk(-h,m));
		candy2[t].push_back(mk(-h,m));
	}
	sort(candy1[0].begin(),candy1[0].end());
	sort(candy1[1].begin(),candy1[1].end());
	sort(candy2[0].begin(),candy2[0].end());
	sort(candy2[1].begin(),candy2[1].end());
	int pre=-1,ans=0,xx=x;
	while(!candy1[0].empty()&&!candy1[1].empty()){
		pp a1,a2;
		if(candy1[1].empty())a2=mk(-siz,-siz);
		else a2=candy1[1].back();
		if(candy1[0].empty())a1=mk(-siz,-siz);
		else a1=candy1[0].back();
		bool yes=0;
		if(-a1.first<=x&&pre!=0){
			x+=a1.second;
			ans++;
			pre=0;
			yes=1;
			candy1[0].pop_back();
		}
		if(-a2.first<=x&&pre!=1){
			x+=a2.second;
			ans++;
			pre=1;
			yes=1;
			candy1[1].pop_back();
		}
		if(!yes)break;
	}
	int ans1=0;
	pre=-1;
	while(!candy2[0].empty()&&!candy2[1].empty()){
		pp a1,a2;
		if(candy2[1].empty())a2=mk(-siz,-siz);
		else a2=candy2[1].back();
		if(candy2[0].empty())a1=mk(-siz,-siz);
		else a1=candy2[0].back();
		bool yes=0;
		if(-a2.first<=xx&&pre!=1){
			xx+=a1.second;
			ans1++;
			pre=1;
			yes=1;
			candy2[1].pop_back();
		}
		if(-a1.first<=xx&&pre!=0){
			xx+=a2.second;
			ans1++;
			pre=0;
			yes=1;
			candy2[0].pop_back();
		}
		if(!yes)break;
	}
	printf("%d\n",max(ans1,ans));
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

int n,x;
vpp candy1[2],candy2[2];
int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	ri(n),ri(x);
	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		candy1[t].push_back(mk(-h,m));
		candy2[t].push_back(mk(-h,m));
	}
	sort(candy1[0].begin(),candy1[0].end());
	sort(candy1[1].begin(),candy1[1].end());
	sort(candy2[0].begin(),candy2[0].end());
	sort(candy2[1].begin(),candy2[1].end());
	int pre=-1,ans=0,xx=x;
	while(!candy1[0].empty()&&!candy1[1].empty()){
		pp a1,a2;
		if(candy1[1].empty())a2=mk(-siz,-siz);
		else a2=candy1[1].back();
		if(candy1[0].empty())a1=mk(-siz,-siz);
		else a1=candy1[0].back();
		bool yes=0;
		if(-a1.first<=x&&pre!=0){
			x+=a1.second;
			ans++;
			pre=0;
			yes=1;
			candy1[0].pop_back();
		}else if(-a2.first<=x&&pre!=1){
			x+=a2.second;
			ans++;
			pre=1;
			yes=1;
			candy1[1].pop_back();
		}
		if(!yes)break;
	}
	int ans1=0;
	while(!candy2[0].empty()&&!candy2[1].empty()){
		pp a1,a2;
		if(candy2[1].empty())a2=mk(-siz,-siz);
		else a2=candy2[1].back();
		if(candy2[0].empty())a1=mk(-siz,-siz);
		else a1=candy2[0].back();
		bool yes=0;
		if(-a2.first<=xx&&pre!=1){
			xx+=a1.second;
			ans1++;
			pre=1;
			yes=1;
			candy2[1].pop_back();
		}else if(-a1.first<=xx&&pre!=0){
			xx+=a2.second;
			ans1++;
			pre=0;
			yes=1;
			candy2[0].pop_back();
		}
		if(!yes)break;
	}
	printf("%d\n",max(ans1,ans));
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<time.h>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<stdlib.h>
#include<bitset>

#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define f1n(i,n) for(int (i) = 1;(i) <= (n);(i)++)
#define mk(f,s) make_pair((f),(s))

#define defV(dp,e) memset(dp,e,sizeof dp)

#define ri(n) scanf("%d",&n)
#define rl(n) scanf("%lld",&n)
#define rs(s) scanf("%s",s)

#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef pair<int ,int > pp;

typedef vector<int  > v1;
typedef vector<ll  > vl;
typedef vector<pp  > vpp;
typedef vector<bool  > vb;
typedef vector<char > vc;
typedef vector<v1 > v2;

typedef map<int ,int > mf;
typedef map<int ,vpp > mv;
typedef map<int ,v1 > ad;

int const siz = 1e9,bound = 5e5,mod = siz+7;
ll const oo=1e18;

set<pp > mass[2][2][2001];
bool del[2][2001];

set<pp>::iterator giv(int trv,int typ,int hh){
	set<pp >::iterator tt=mass[trv][typ][hh].end(),cop;
	tt--;
	cop=tt;
	while(del[trv][tt->second]&&tt!=mass[trv][typ][hh].begin()){
		tt--;
		mass[trv][typ][hh].erase(cop);
		cop=tt;
	}
	if(tt==mass[trv][typ][hh].begin()&&del[trv][tt->second])return tt=mass[trv][typ][hh].end();
	del[trv][tt->second]=1;
	return tt;
}

int main() {
//	freopen("o.txt","w",stdout);
//	freopen("in.txt","r",stdin);
//	ios::sync_with_stdio(0);
	int n,x;
	ri(n),ri(x);

	fn(i,n){
		int t,h,m;
		ri(t),ri(h),ri(m);
		for(int j=h;j<2001;j++){
			mass[0][t][j].insert(mk(m,i));
			mass[1][t][j].insert(mk(m,i));
		}
	}
	int pre=-1,res1=0,xx=x,trv=0;
	while(1){
		int hh=x;
		if(hh>2000)hh=2000,x=2000;
		bool yes=0;
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				x+=tt->first;
				res1++;
				pre=0;
				yes=1;
			}
		}
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				x+=tt->first;
				res1++;
				pre=1;
				yes=1;
			}
		}
		if(!yes)break;
	}

	int res2=0;
	pre=-1;
	trv=1;

	while(1){
		int hh=xx;
		if(hh>2000)hh=2000,xx=2000;
		bool yes=0;
		if(pre!=1){
			set<pp>::iterator tt=giv(trv,1,hh);
			if(tt!=mass[trv][1][hh].end()){
				xx+=tt->first;
				res2++;
				pre=1;
				yes=1;
			}
		}
		if(pre!=0){
			set<pp>::iterator tt=giv(trv,0,hh);
			if(tt!=mass[trv][0][hh].end()){
				xx+=tt->first;
				res2++;
				pre=0;
				yes=1;
			}
		}
		if(!yes)break;
	}
	printf("%d\n",max(res1,res2));
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag;
	if(a.size()>b.size())
		flag=1;
	else
		flag=0;
	for(;;){
		int t=0;
		if(flag){
			for(int i=1;i<a.size();i++)
				if(a[i].first<=x && a[t].second<a[i].second)
					t=i;
			if(a[t].first<=x && a[t].second!=-1){
				x+=a[t].second;
				ans++;
				flag=!flag;
				a[t].second=-1;
			}else
				if(!ans)
					flag=!flag;
				else
					break;
		}else{
			for(int i=1;i<b.size();i++)
				if(b[i].first<=x && b[t].second<b[i].second)
					t=i;
			if(b[t].first<=x && b[t].second!=-1){
				x+=b[t].second;
				ans++;
				flag=!flag;
				b[t].second=-1;
			}else
				if(!ans)
					flag=!flag;
				else
					break;
		}
	}
	printf("%d",ans);
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag,r=0;
	if(a.size()>b.size())
		flag=1;
	else
		flag=0;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].second!=-1 && a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].second!=-1 && b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	printf("%d",ans);
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int n,x,ans=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	char flag;
	if(a[0].first<=x && b[0].first<=x){
		if(a.size()>b.size())
			flag=0;
		else
			flag=1;
	}else{
		if(a[0].first>x)
			flag=1;
		else
			flag=0;
	}
	for(;;){
		int t=0;
		if(!flag){
			for(int i=0;i<a.size() && a[i].first<=x;i++)
				if(a[t].second<a[i].second)
					t=i;
			if(a[t].first>x || a[t].second==-1)
				break;
			ans++;
			x+=a[t].second;
			flag=!flag;
			a[t].second=-1;
		}else{
			for(int i=0;i<b.size() && b[i].first<=x;i++)
				if(b[t].second<b[i].second)
					t=i;
			if(b[t].first>x || b[t].second==-1)
				break;
			ans++;
			x+=b[t].second;
			flag=!flag;
			b[t].second=-1;
		}
	}
	printf("%d",ans);
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0,ans1=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag=1,r=0;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].second!=-1 && a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].second!=-1 && b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	flag=0;
	r=0;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].second!=-1 && a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-1;
				x+=mx;
				flag=!flag;
				ans1++;
			}else
				if(!ans1 && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].second!=-1 && b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-1;
				x+=mx;
				flag=!flag;
				ans1++;
			}else
				if(!ans1 && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	printf("%d",max(ans,ans1));
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag,r=0;
	if(a.size()>b.size())
		flag=1;
	else
		flag=0;
	for(;;){
		int t=0;
		if(flag){
			for(int i=1;i<a.size();i++)
				if(a[i].first<=x && a[t].second<a[i].second)
					t=i;
			if(a[t].first<=x && a[t].second!=-1){
				x+=a[t].second;
				ans++;
				flag=!flag;
				a[t].second=-1;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=1;i<b.size();i++)
				if(b[i].first<=x && b[t].second<b[i].second)
					t=i;
			if(b[t].first<=x && b[t].second!=-1){
				x+=b[t].second;
				ans++;
				flag=!flag;
				b[t].second=-1;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	printf("%d",ans);
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0,ans1=0,v;
	scanf("%d%d",&n,&x);
	v=x;
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag=1,r=0;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-a[t].second;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-b[t].second;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!ans && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	flag=0;
	r=0;
	x=v;
	for(int i=0;i<a.size();i++)
		if(a[i].second<0)
			a[i].second=-a[i].second;
	for(int i=0;i<b.size();i++)
		if(b[i].second<0)
			b[i].second=-b[i].second;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-a[t].second;
				x+=mx;
				flag=!flag;
				ans1++;
			}else
				if(!ans1 && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-b[t].second;
				x+=mx;
				flag=!flag;
				ans1++;
			}else
				if(!ans1 && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	printf("%d",max(ans,ans1));
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n,x,ans=0;
	scanf("%d%d",&n,&x);
	vector<pair<int,int>>a,b;
	for(int i=0;i<n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t)
			a.push_back(make_pair(h,m));
		else
			b.push_back(make_pair(h,m));
	}
	if(a.empty() || b.empty()){
		if(a.empty()){
			if(b[0].first<=x)
				printf("1");
			else
				printf("0");
		}else
			if(a[0].first<=x)
				printf("1");
			else
				printf("0");
		return 0;
	}
	char flag,r=0;
	if(a.size()>b.size())
		flag=1;
	else
		flag=0;
	for(;;){
		int t=-1,mx=0;
		if(flag){
			for(int i=0;i<a.size();i++)
				if(a[i].second!=-1 && a[i].first<=x && a[i].second>mx){
					mx=a[i].second;
					t=i;
				}
			if(mx){
				a[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!flag && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}else{
			for(int i=0;i<b.size();i++)
				if(b[i].second!=-1 && b[i].first<=x && b[i].second>mx){
					mx=b[i].second;
					t=i;
				}
			if(mx){
				b[t].second=-1;
				x+=mx;
				flag=!flag;
				ans++;
			}else
				if(!flag && !r){
					flag=!flag;
					r=1;
				}else
					break;
		}
	}
	printf("%d",ans);
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<functional>
#include<cmath>
#include<vector>

using namespace std;

struct candy{
	int t;
	int h, m;
};

candy in[2005];

int n,x;

bool operator>(const candy &c1, const candy &c2)
{
	return c1.m>c2.m;
}

bool operator<(const candy &c1, const candy &c2)
{
	return c1.m<c2.m;
}

bool cmp(const candy &c1, const candy &c2)
{
	return c1.h<c2.h;
}

priority_queue<candy> que;

int main()
{
	scanf("%d%d", &n, &x);
	int x1=x;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d", &in[i].t, &in[i].h, &in[i].m);
	}

	sort(in, in+n, cmp);
	int i,j,k,ans=0, ans1=0, first;
	for( i=0;i<n;i++)
	{
		if(in[i].h<=x)
			que.push(in[i]);
		else
			break;
	}
	int pre;
	while(!que.empty())
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x &&( ans==0 || (ans!=0 && t.t!=pre))){
			if (ans==0)
				first=t.t;
			pre=t.t;
			ans++;
			x+=t.m;

//			printf("%d ", t.m);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x)
				que.push(in[i]);
			else
				break;
		}
	}

//	printf("\n ans= %d\n", ans);

	for( i=0;i<n;i++)
	{
		if(in[i].h<=x1)
			que.push(in[i]);
		else
			break;
	}

	vector<candy> tmplist;

	while(!que.empty())
	{
		if(que.top().t==first){
			tmplist.push_back(que.top());
			que.pop();
		}
		else
			break;
	}
	bool redo=true;
	if(que.empty())
		redo=false;
	else{
		candy t=que.top();
		ans1++;
		x1+=t.m;
		pre=t.t;
//		printf("%d ", t.m);
		que.pop();
		for(int i=0;i<tmplist.size();i++)
			que.push(tmplist[i]);
	}

	while(!que.empty() && redo)
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x1 && t.t!=pre)
		{
			pre=t.t;
			ans1++;
			x1+=t.m;

//			printf("%d ", t.m);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x1)
				que.push(in[i]);
			else
				break;
		}
	}
//	printf("\n ans1= %d\n", ans1);

	ans = max(ans, ans1);
	printf("%d\n", ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<functional>
#include<cmath>
#include<vector>

using namespace std;

struct candy{
	int t;
	int h, m;
};

candy in[3005];

int n,x;

//bool operator>(const candy &c1, const candy &c2)
//{
//	return c1.m>c2.m;
//}

bool operator<(const candy &c1, const candy &c2)
{
	return c1.m<c2.m;
}

bool cmp(const candy &c1, const candy &c2)
{
	return c1.h<c2.h;
}

priority_queue<candy> que;

int main()
{
	scanf("%d%d", &n, &x);
	int x1=x;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d", &in[i].t, &in[i].h, &in[i].m);
	}

	sort(in, in+n, cmp);
	int i,j,k,ans=0, ans1=0, first;
	for( i=0;i<n;i++)
	{
		if(in[i].h<=x)
			que.push(in[i]);
		else
			break;
	}
	int pre;
	while(!que.empty())
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x &&( ans==0 || (ans!=0 && t.t!=pre))){
			if (ans==0)
				first=t.t;
			pre=t.t;
			ans++;
			x+=t.m;

//			printf("%d ", t.m);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x)
				que.push(in[i]);
			else
				break;
		}
	}

//	printf("\n ans= %d\n", ans);

	for( i=0;i<n;i++)
	{
		if(in[i].h<=x1)
			que.push(in[i]);
		else
			break;
	}

	vector<candy> tmplist;

	while(!que.empty())
	{
		if(que.top().t==first){
			tmplist.push_back(que.top());
			que.pop();
		}
		else
			break;
	}
	bool redo=true;
	if(que.empty())
		redo=false;
	else{
		candy t=que.top();
		ans1++;
		x1+=t.m;
		pre=t.t;
//		printf("%d ", t.m);
		que.pop();
		for(int i=0;i<tmplist.size();i++)
			que.push(tmplist[i]);
	}

	while(!que.empty() && redo)
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x1 && t.t!=pre)
		{
			pre=t.t;
			ans1++;
			x1+=t.m;

//			printf("%d ", t.m);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x1)
				que.push(in[i]);
			else
				break;
		}
	}
//	printf("\n ans1= %d\n", ans1);

	ans = max(ans, ans1);
	printf("%d\n", ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<functional>
#include<cmath>
#include<vector>

using namespace std;

struct candy{
	int t;
	int h, m;
};

candy in[3005];

int n,x;

//bool operator>(const candy &c1, const candy &c2)
//{
//	return c1.m>c2.m;
//}

bool operator<(const candy &c1, const candy &c2)
{
	return c1.m<c2.m;
}

bool cmp(const candy &c1, const candy &c2)
{
	return c1.h<c2.h;
}

priority_queue<candy> que;

int main()
{
	scanf("%d%d", &n, &x);
	int x1=x;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d", &in[i].t, &in[i].h, &in[i].m);
	}

	sort(in, in+n, cmp);
	int i,j,k,ans=0, ans1=0, first;
	for( i=0;i<n;i++)
	{
		if(in[i].h<=x)
			que.push(in[i]);
		else
			break;
	}
	int pre;

	vector<candy> list;

	while(!que.empty())
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x &&( ans==0 || (ans!=0 && t.t!=pre))){
			if (ans==0)
				first=t.t;
			pre=t.t;
			ans++;
			x+=t.m;
			for(int i=0;i<list.size();i++)
				que.push(list[i]);
			list.clear();
//			printf("%d ", t.m);
		}
		else{
			list.push_back(t);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x)
				que.push(in[i]);
			else
				break;
		}
	}

//	printf("\n ans= %d\n", ans);

	list.clear();

	for( i=0;i<n;i++)
	{
		if(in[i].h<=x1)
			que.push(in[i]);
		else
			break;
	}

	vector<candy> tmplist;

	while(!que.empty())
	{
		if(que.top().t==first){
			tmplist.push_back(que.top());
			que.pop();
		}
		else
			break;
	}
	bool redo=true;
	if(que.empty())
		redo=false;
	else{
		candy t=que.top();
		ans1++;
		x1+=t.m;
		pre=t.t;
//		printf("%d ", t.m);
		que.pop();
		for(int i=0;i<tmplist.size();i++)
			que.push(tmplist[i]);
	}

	while(!que.empty() && redo)
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x1 && t.t!=pre)
		{

			pre=t.t;
			ans1++;
			x1+=t.m;

			for(int i=0;i<list.size();i++)
				que.push(list[i]);
			list.clear();

//			printf("%d ", t.m);
		}else
		{
			list.push_back(t);
		}

		for( ;i<n;i++)
		{
			if(in[i].h<=x1)
				que.push(in[i]);
			else
				break;
		}
	}
//	printf("\n ans1= %d\n", ans1);

	ans = max(ans, ans1);
	printf("%d\n", ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<functional>
#include<cmath>
#include<vector>

using namespace std;

struct candy{
	int t;
	int h, m;
};

candy in[2005];

int n,x;

bool operator>(const candy &c1, const candy &c2)
{
	return c1.m>c2.m;
}

bool operator<(const candy &c1, const candy &c2)
{
	return c1.m<c2.m;
}

bool cmp(const candy &c1, const candy &c2)
{
	return c1.h<c2.h;
}

priority_queue<candy> que;

int main()
{
	scanf("%d%d", &n, &x);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d", &in[i].t, &in[i].h, &in[i].m);
	}

	sort(in, in+n, cmp);
	int i,j,k,ans=0;
	for( i=0;i<n;i++)
	{
		if(in[i].h<=x)
			que.push(in[i]);
		else
			break;
	}
	int pre;
	while(!que.empty())
	{
		candy t=que.top();
		que.pop();
		if(t.h<=x &&( ans==0 || (ans!=0 && t.t!=pre))){
			pre=t.t;
			ans++;
			x+=t.m;

		//	printf("%d ", t.m);
		}
		for( ;i<n;i++)
		{
			if(in[i].h<=x)
				que.push(in[i]);
			else
				break;
		}
	}

	printf("%d\n", ans);

	return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <stdio.h>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x70707070;
const ll MAXLONG = (ll) 1 << 60;

priority_queue<int> pq[2];
vector<pii> arr[2];
int n, x;

void init() {
	up(i, 0, 2) {
		while (pq[i].empty() == false) {
			pq[i].pop();
		}
	}
}

int calc(int tp) {
	init();

	int hei = x;
	int pos[2];
	pos[0] = pos[1] = 0;
	int idx = tp;
	int res = 0;

	while (true) {

		while (pos[idx] < arr[idx].size() && hei >= arr[idx][ pos[idx] ].first) {
			pii pa = arr[idx][ pos[idx] ];
			pq[idx].push(pa.second);
			pos[idx] ++;
		}

		if (pq[idx].size() == 0) {
			break;
		}

		hei += pq[idx].top();
		pq[idx].pop();
		idx = 1 - idx;

		res ++;
	}

	return res;
}

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (rd2(n, x) != -1) {
		arr[0].clear();
		arr[1].clear();

		up(i, 0, n) {
			int t, h, m;
			rd3(t, h, m);
			arr[t].push_back(make_pair(h, m));
		}

		sort(arr[0].begin(), arr[0].end());
		sort(arr[1].begin(), arr[1].end());

		int r1 = calc(0);
		int r2 = calc(1);
		int res = max(r1, r2);
		cout << res << endl;
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <stdio.h>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x70707070;
const ll MAXLONG = (ll) 1 << 60;

vector<pii > arr[2];
int pos[2];
int n, x;

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (rd2(n, x) != -1) {
		arr[0].clear();
		arr[1].clear();

		up(i, 0, n) {
			int t, h, m;
			rd3(t, h, m);
			arr[t].push_back(make_pair(h, m));
		}

		sort(arr[0].begin(), arr[0].end());
		sort(arr[1].begin(), arr[1].end());

		int idx = 1;
		if (arr[1].size() == 0 || (arr[0].size() && arr[0][0].first < arr[1][0].first)) {
			idx = 0;
		}

		int res = 0;
		int hei = x;
		pos[0] = pos[1] = 0;
		while (true) {
			if (pos[idx] >= arr[idx].size() || arr[idx][ pos[idx] ].first > hei) {
				break;
			}

			hei += arr[idx][ pos[idx] ].second;
			pos[idx] ++;
			idx = 1 - idx;

			res ++;
		}

		cout << res << endl;
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <stdio.h>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x70707070;
const ll MAXLONG = (ll) 1 << 60;

priority_queue<int> pq[2];
vector<pii> arr[2];
int n, x;

int calc(int tp) {
	int hei = x;
	int pos[2];
	int idx = tp;
	int res = 0;
	pos[0] = pos[1] = 0;
	while (true) {
		//while ( pos[idx] >= arr[idx].size() || arr[idx][ pos[idx] ].first > hei ) {
		//	break;
		//}

		while (pos[idx] < arr[idx].size() && hei >= arr[idx][ pos[idx] ].first) {
			pii pa = arr[idx][ pos[idx] ];
			pq[idx].push(pa.second);
			pos[idx] ++;
		}

		if (pq[idx].size() == 0) {
			break;
		}

		hei += pq[idx].top();
		pq[idx].pop();
		idx = 1 - idx;

		res ++;
	}

	return res;
}

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (rd2(n, x) != -1) {
		arr[0].clear();
		arr[1].clear();

		up(i, 0, n) {
			int t, h, m;
			rd3(t, h, m);
			arr[t].push_back(make_pair(h, m));
		}

		sort(arr[0].begin(), arr[0].end());
		sort(arr[1].begin(), arr[1].end());

		int r1 = calc(0);
		int r2 = calc(1);
		int res = max(r1, r2);
		cout << res << endl;
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <stdio.h>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x70707070;
const ll MAXLONG = (ll) 1 << 60;

vector<pii > arr[2];
int pos[2];
int n, x;

bool cmp(const pii &p1, const pii &p2) {
	if (p1.first != p2.first) {
		return p1.first < p2.first;
	} else {
		return p1.second > p2.second;
	}
}

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (rd2(n, x) != -1) {
		arr[0].clear();
		arr[1].clear();

		up(i, 0, n) {
			int t, h, m;
			rd3(t, h, m);
			arr[t].push_back(make_pair(h, m));
		}

		sort(arr[0].begin(), arr[0].end(), cmp);
		sort(arr[1].begin(), arr[1].end(), cmp);

		int idx = 1;
		if (arr[1].size() == 0 || (arr[0].size() && arr[0][0].first < arr[1][0].first)) {
			idx = 0;
		}

		int res = 0;
		int hei = x;
		pos[0] = pos[1] = 0;
		while (true) {
			if (pos[idx] >= arr[idx].size() || arr[idx][ pos[idx] ].first > hei) {
				break;
			}

			hei += arr[idx][ pos[idx] ].second;
			pos[idx] ++;
			idx = 1 - idx;

			res ++;
		}

		cout << res << endl;
	}
    return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,x;

struct my
{
	int high,mass;
	my(int _high,int _mass)
	{
		high=_high;
		mass=_mass;
	}
};

vector<my> v0,v1;
int  find0(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v0.size();i++) if (v0[i].high!=-1)
	{
		if (v0[i].high<=high && v0[i].mass > h)
		{
			h=v0[i].mass;
			ans=i;
		}
	}
	v0[ans].high=-1;
	return ans;
}

int  find1(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v1.size();i++) if (v1[i].high!=-1)
	{
		if (v1[i].high<=high && v1[i].mass > h)
		{
			h=v1[i].mass;
			ans=i;
		}
	}
	v1[ans].high=-1;
	return ans;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n>>x;

	int type,high,mass;

	for (i=0;i<n;i++)
	{
		cin>>type>>high>>mass;
		if (type==0)
		{
			v0.push_back(my(high,mass));
		}
		else
		{
			v1.push_back(my(high,mass));
		}
	}

	int ans=0;

	k=x;
	int num=0;
	while (true)
	{
		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;
	}
	ans=max(ans,num);

	num=0;
	while (true)
	{
		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;

		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

	}
	ans=max(ans,num);

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,x;

struct my
{
	int high,mass;
	int vis;
	my(int _high,int _mass,int _vis)
	{
		high=_high;
		mass=_mass;
		vis=_vis;
	}
};

vector<my> v0,v1;
int  find0(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v0.size();i++) if (!v0[i].vis)
	{
		if (v0[i].high<=high && v0[i].mass > h)
		{
			h=v0[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v0[ans].vis=1;
	return ans;
}

int  find1(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v1.size();i++) if (!v1[i].vis)
	{
		if (v1[i].high<=high && v1[i].mass > h)
		{
			h=v1[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v1[ans].vis=1;
	return ans;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n>>x;

	int type,high,mass;

	for (i=0;i<n;i++)
	{
		cin>>type>>high>>mass;
		if (type==0)
		{
			v0.push_back(my(high,mass,0));
		}
		else
		{
			v1.push_back(my(high,mass,0));
		}
	}

	int ans=0;

	k=x;
	int num=0;
	while (true)
	{
		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;
	}
	ans=max(ans,num);
	//cout<<ans<<endl;
	num=0;
	while (true)
	{
		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;

		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

	}
	ans=max(ans,num);

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,x;

struct my
{
	int high,mass;
	my(int _high,int _mass)
	{
		high=_high;
		mass=_mass;
	}
};

vector<my> v0,v1;
int  find0(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v0.size();i++) if (v0[i].high!=-1)
	{
		if (v0[i].high<=high && v0[i].mass > h)
		{
			h=v0[i].mass;
			ans=i;
		}
	}
	if (ans>=0)
	v0[ans].high=-1;
	return ans;
}

int  find1(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v1.size();i++) if (v1[i].high!=-1)
	{
		if (v1[i].high<=high && v1[i].mass > h)
		{
			h=v1[i].mass;
			ans=i;
		}
	}
	if (ans>=0)
	v1[ans].high=-1;
	return ans;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n>>x;

	int type,high,mass;

	for (i=0;i<n;i++)
	{
		cin>>type>>high>>mass;
		if (type==0)
		{
			v0.push_back(my(high,mass));
		}
		else
		{
			v1.push_back(my(high,mass));
		}
	}

	int ans=0;

	k=x;
	int num=0;
	while (true)
	{
		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;
	}
	ans=max(ans,num);
	//cout<<ans<<endl;
	num=0;
	while (true)
	{
		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;

		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

	}
	ans=max(ans,num);

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,x;

struct my
{
	int high,mass;
	int vis;
	my(int _high,int _mass,int _vis)
	{
		high=_high;
		mass=_mass;
		vis=_vis;
	}
};

vector<my> v0,v1;
int  find0(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v0.size();i++) if (!v0[i].vis)
	{
		if (v0[i].high<=high && v0[i].mass > h)
		{
			h=v0[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v0[ans].vis=1;
	return ans;
}

int  find1(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v1.size();i++) if (!v1[i].vis)
	{
		if (v1[i].high<=high && v1[i].mass > h)
		{
			h=v1[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v1[ans].vis=1;
	return ans;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n>>x;

	int type,high,mass;

	for (i=0;i<n;i++)
	{
		cin>>type>>high>>mass;
		if (type==0)
		{
			v0.push_back(my(high,mass,0));
		}
		else
		{
			v1.push_back(my(high,mass,0));
		}
	}

	int ans=0;

	k=x;
	int num=0;
	while (true)
	{
		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;
	}
	ans=max(ans,num);
	//cout<<ans<<endl;

	for (i=0;i<v0.size();i++) v0[i].vis=0;
	for (i=0;i<v1.size();i++) v1[i].vis=0;
	num=0;
	while (true)
	{
		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;

		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

	}
	ans=max(ans,num);

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,x;

struct my
{
	int high,mass;
	int vis;
	my(int _high,int _mass,int _vis)
	{
		high=_high;
		mass=_mass;
		vis=_vis;
	}
};

vector<my> v0,v1;
int  find0(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v0.size();i++) if (!v0[i].vis)
	{
		if (v0[i].high<=high && v0[i].mass > h)
		{
			h=v0[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v0[ans].vis=1;
	return ans;
}

int  find1(int high)
{
	int h=0;
	int ans=-1;
	for (int i=0;i<v1.size();i++) if (!v1[i].vis)
	{
		if (v1[i].high<=high && v1[i].mass > h)
		{
			h=v1[i].mass;
			ans=i;
		}
	}
	if (ans>=0) v1[ans].vis=1;
	return ans;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n>>x;

	int type,high,mass;

	for (i=0;i<n;i++)
	{
		cin>>type>>high>>mass;
		if (type==0)
		{
			v0.push_back(my(high,mass,0));
		}
		else
		{
			v1.push_back(my(high,mass,0));
		}
	}

	int ans=0;

	k=x;
	int num=0;
	while (true)
	{
		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;
	}
	ans=max(ans,num);
	//cout<<ans<<endl;

	for (i=0;i<v0.size();i++) v0[i].vis=0;
	for (i=0;i<v1.size();i++) v1[i].vis=0;
	num=0;
	k=x;
	while (true)
	{
		j=find1(k);
		if (j==-1) break;
		num++;
		k+=v1[j].mass;

		j=find0(k);
		if (j==-1) break;
		num++;
		k+=v0[j].mass;

	}
	ans=max(ans,num);

	cout<<ans<<endl;

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <functional>
#include <time.h>
#include <cmath>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

__int64 EPS = 1000000007;
double PI = 3.14159265358979323846;

__int64 abss(__int64 h)
{
	if (h < 0)
	{
		return -h;
	}
	return h;
}

__int64 ceill(__int64 x, __int64 y)
{
	if (x % y != 0)
	{
		return (x / y) + 1;
	}
	return x / y;
}

__int64 binpow (__int64 a, int n) {
	if (n == 0)
	{
		return 1;
	}
	if (n % 2 == 1)
	{
		return binpow(a, n - 1) * a;
	}
	else
	{
		ll b = binpow(a, n / 2);
		return b * b;
	}
}

const int MAXN = 100010;

int a[MAXN];
int b[MAXN];

vector <pair<int, pair<int, int > > > p;

int main()
{
	int n, m;
	cin >> n >> m;
	int t, x, y;
	int q = m;
	for (int i = 0; i < n; i++)
	{
		cin >> t >> x >> y;
		p.push_back(mp(x, mp(y, t)));
	}
	int prev = 0;
	int answer = 0;
	for (int i = 0; i < n; i++)
	{
		int index = -1;
		int y = 0;
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			if (y < p[j].second.first && m >= p[j].first)
			{
				y = p[j].second.first;
				index = j;
			}
		}
		if (index != -1)
		{
			m += y;
			b[index] = 1;
			answer++;
			prev = abss(prev - 1);
		}
	}
	prev = 1;
	int answer1 = answer;
	answer = 0;
	m = q;
	for (int i = 0; i < n; i++)
	{
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		int index = -1;
		int y = 0;
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			if (y < p[j].second.first && m >= p[j].first)
			{
				y = p[j].second.first;
				index = j;
			}
		}
		if (index != -1)
		{
			m += y;
			b[index] = 1;
			answer++;
			prev = abss(prev - 1);
		}
	}
	cout << max(answer, answer1);
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <functional>
#include <time.h>
#include <cmath>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

__int64 EPS = 1000000007;
double PI = 3.14159265358979323846;

__int64 abss(__int64 h)
{
	if (h < 0)
	{
		return -h;
	}
	return h;
}

__int64 ceill(__int64 x, __int64 y)
{
	if (x % y != 0)
	{
		return (x / y) + 1;
	}
	return x / y;
}

__int64 binpow (__int64 a, int n) {
	if (n == 0)
	{
		return 1;
	}
	if (n % 2 == 1)
	{
		return binpow(a, n - 1) * a;
	}
	else
	{
		ll b = binpow(a, n / 2);
		return b * b;
	}
}

const int MAXN = 100010;

int a[MAXN];
int b[MAXN];

vector <pair<int, pair<int, int > > > p;

int main()
{
	int n, m;
	cin >> n >> m;
	int t, x, y;
	int q = m;
	for (int i = 0; i < n; i++)
	{
		cin >> t >> x >> y;
		p.push_back(mp(x, mp(y, t)));
	}
	int prev = 0;
	int answer = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			m = m + p[j].second.first;
			answer++;
			b[j] = 1;
			prev = p[j].second.second;
		}
	}
	prev = 1;
	int answer1 = answer;
	answer = 0;
	m = q;
	for (int i = 0; i < n; i++)
	{
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			m = m + p[j].second.first;
			answer++;
			b[j] = 1;
			prev = p[j].second.second;
		}
	}
	cout << max(answer, answer1);
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <functional>
#include <time.h>
#include <cmath>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

__int64 EPS = 1000000007;
double PI = 3.14159265358979323846;

__int64 abss(__int64 h)
{
	if (h < 0)
	{
		return -h;
	}
	return h;
}

__int64 ceill(__int64 x, __int64 y)
{
	if (x % y != 0)
	{
		return (x / y) + 1;
	}
	return x / y;
}

__int64 binpow (__int64 a, int n) {
	if (n == 0)
	{
		return 1;
	}
	if (n % 2 == 1)
	{
		return binpow(a, n - 1) * a;
	}
	else
	{
		ll b = binpow(a, n / 2);
		return b * b;
	}
}

const int MAXN = 100010;

int a[MAXN];
int b[MAXN];

vector <pair<int, pair<int, int > > > p;

int main()
{
	int n, m;
	cin >> n >> m;
	int t, x, y;
	int q = m;
	for (int i = 0; i < n; i++)
	{
		cin >> t >> x >> y;
		p.push_back(mp(x, mp(y, t)));
	}
	int prev = 0;
	int answer = 0;
	for (int i = 0; i < n; i++)
	{
		int index = -1;
		int y = 0;
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			if (y < p[j].first && m >= p[j].first)
			{
				y = p[j].first;
				index = j;
			}
		}
		if (index != -1)
		{
			m += y;
			b[index] = 1;
			answer++;
			prev = abss(prev - 1);
		}
	}
	prev = 1;
	int answer1 = answer;
	answer = 0;
	m = q;
	for (int i = 0; i < n; i++)
	{
		b[i] = 0;
	}
	for (int i = 0; i < n; i++)
	{
		int index = -1;
		int y = 0;
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			if (y < p[j].first && m >= p[j].first)
			{
				y = p[j].first;
				index = j;
			}
		}
		if (index != -1)
		{
			m += y;
			b[index] = 1;
			answer++;
			prev = abss(prev - 1);
		}
	}
	cout << max(answer, answer1);
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <functional>
#include <time.h>
#include <cmath>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

__int64 EPS = 1000000007;
double PI = 3.14159265358979323846;

__int64 abss(__int64 h)
{
	if (h < 0)
	{
		return -h;
	}
	return h;
}

__int64 ceill(__int64 x, __int64 y)
{
	if (x % y != 0)
	{
		return (x / y) + 1;
	}
	return x / y;
}

__int64 binpow (__int64 a, int n) {
	if (n == 0)
	{
		return 1;
	}
	if (n % 2 == 1)
	{
		return binpow(a, n - 1) * a;
	}
	else
	{
		ll b = binpow(a, n / 2);
		return b * b;
	}
}

const int MAXN = 100010;

int a[MAXN];
int b[MAXN];

vector <pair<int, pair<int, int > > > p;

int main()
{
	int n, m;
	cin >> n >> m;
	int t, x, y;
	for (int i = 0; i < n; i++)
	{
		cin >> t >> x >> y;
		p.push_back(mp(x, mp(y, t)));
	}
	sort(p.begin(), p.end());
	int prev = -1;
	int answer = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (b[j] == 1)
			{
				continue;
			}
			if (m < p[j].first)
			{
				continue;
			}
			if (prev == p[j].second.second)
			{
				continue;
			}
			m = m + p[j].second.first;
			answer++;
			b[j] = 1;
			prev = p[j].second.second;
		}
	}
	cout << answer;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

bool eat[2100];

int main() {

	int n,x,x_;
	cin>>n>>x; x_=x;

	vector<pair<int,pair<int,int>>> C(n);
	for (int i=0 ; i<n ; i++)
		cin >> C[i].first >> C[i].second.first >> C[i].second.second;

	for (int i = 0; i<n; i++) eat[i] = false;
	bool chk=false;
	int last=1, ans=0;
	do {
		chk = false;
		for (int i = 0; i < n; i++) {
			if (eat[i]) continue;
			if (x >= C[i].second.first && last != C[i].first) {
				eat[i]=true, chk=true;
				last^=1;
				x += C[i].second.second;
				ans++;
			}
		}
	} while (chk);

	for (int i = 0; i<n; i++) eat[i]=false;
	x=x_, chk = false, last = 0;
	int ans2 = 0;
	do {
		chk = false;
		for (int i = 0; i < n; i++) {
			if (eat[i]) continue;
			if (x >= C[i].second.first && last != C[i].first) {
				eat[i] = true, chk = true;
				last ^= 1;
				x += C[i].second.second;
				ans2++;
			}
		}
	} while (chk);

	if (ans>ans2) cout<<ans<<endl;
	else cout<<ans2<<endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

bool eat[2100];

int main() {

    int n;
    long long int x,x_;
    cin>>n>>x; x_=x;

    vector<pair<long long int,pair<long long int, long long int>>> C(n);
    for (int i=0 ; i<n ; i++)
        cin >> C[i].first >> C[i].second.first >> C[i].second.second;

    for (int i = 0; i<n; i++) eat[i] = false;
    bool chk=false;
    long long int last=1, ans=0;
    do {
        chk = false;
        for (int i = 0; i < n; i++) {
            if (eat[i]) continue;
            if (x >= C[i].second.first && last != C[i].first) {
                eat[i]=true, chk=true;
                last^=1;
                x += C[i].second.second;
                ans++;
            }
        }
    } while (chk);

    for (int i = 0; i<n; i++) eat[i]=false;
    x=x_, chk = false, last = 0;
    long long int ans2 = 0;
    do {
        chk = false;
        for (int i = 0; i < n; i++) {
            if (eat[i]) continue;
            if (x >= C[i].second.first && last != C[i].first) {
                eat[i] = true, chk = true;
                last ^= 1;
                x += C[i].second.second;
                ans2++;
            }
        }
    } while (chk);

    if (ans>ans2) cout<<ans<<endl;
    else cout<<ans2<<endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

bool eat[2100];

int main() {

    int n;
    long long int x,x_;
    cin>>n>>x; x_=x;

    vector<pair<long long int,pair<long long int, long long int>>> C(n);
    for (int i=0 ; i<n ; i++)
        cin >> C[i].first >> C[i].second.first >> C[i].second.second;

    for (int i = 0; i<n; i++) eat[i] = false;
    bool chk=false;
    long long int last=1, ans=0;
    do {
        chk = false;
        int index = -1;
        long long int mass = -1;

        for (int i = 0; i < n; i++) {
            if (eat[i]) continue;
            if (x >= C[i].second.first && last != C[i].first) {
                if (C[i].second.second > mass) {
                    mass = C[i].second.second;
                    index=i;
                }
            }
        }
        if (index == -1) break;

        eat[index] = true, chk = true;
        last ^= 1;
        x += mass;
        ans++;
    } while (chk);

    for (int i = 0; i<n; i++) eat[i]=false;
    x=x_, chk = false, last = 0;
    long long int ans2 = 0;
    do {
        chk = false;
        int index = -1;
        long long int mass = -1;

        for (int i = 0; i < n; i++) {
            if (eat[i]) continue;
            if (x >= C[i].second.first && last != C[i].first) {
                if (C[i].second.second > mass) {
                    mass = C[i].second.second;
                    index = i;
                }
            }
        }
        if (index == -1) break;

        eat[index] = true, chk = true;
        last ^= 1;
        x += mass;
        ans2++;
    } while (chk);

    if (ans>ans2) cout<<ans<<endl;
    else cout<<ans2<<endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

bool eat[2100];

int main() {

	int n,x;
	cin>>n>>x;

	vector<pair<int,pair<int,int>>> C(n);
	for (int i=0 ; i<n ; i++)
		cin >> C[i].first >> C[i].second.first >> C[i].second.second;

	for (int i = 0; i<n; i++) eat[i] = false;
	bool chk=false;
	int last=1, ans=0;
	do {
		chk = false;
		for (int i = 0; i < n; i++) {
			if (eat[i]) continue;
			if (x >= C[i].second.first && last != C[i].first) {
				eat[i]=true, chk=true;
				last^=1;
				x += C[i].second.second;
				ans++;
			}
		}
	} while (chk);

	for (int i = 0; i<n; i++) eat[i]=false;
	chk = false, last = 0;
	int ans2 = 0;
	do {
		chk = false;
		for (int i = 0; i < n; i++) {
			if (eat[i]) continue;
			if (x >= C[i].second.first && last != C[i].first) {
				eat[i] = true, chk = true;
				last ^= 1;
				x += C[i].second.second;
				ans2++;
			}
		}
	} while (chk);

	if (ans>ans2) cout<<ans<<endl;
	else cout<<ans2<<endl;

	return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int n,init;

struct candy {
	int height;
	int mass;
};
candy type0[2002];
candy type1[2002];
bool chk0[2002];
bool chk1[2002];

int n1,n0;
int max;

const bool compare(struct candy &l,struct candy &r)
{
	if(l.mass==r.mass)
		return(l.height>r.height);

	return(l.mass>r.mass);
}
void process()
{
	int type=0,i,j,l;
	int h,cnt=0,res=-1;

	//0���� ����
	h = init;

	for(l=0 ; l<2 ; l++)
	{
		for(i=0 ; i<n ; i++)
		{
			if(type==0)
			{
				for(j=0 ; j<n0 ; j++)
				{
					if(type0[j].height <= h && chk0[j]==false)
					{
						chk0[j]=true;
						h+=type0[j].mass;
						cnt++;
						break;
					}
				}
			}
			else
			{
				for(j=0 ; j<n1 ; j++)
				{
					if(type1[j].height <= h && chk1[j]==false)
					{
						chk1[j]=true;
						h+=type1[j].mass;
						cnt++;
						break;
					}
				}
			}
			type = !type;
		}

		if(res<cnt)
			res=cnt;

		cnt=0;
		type=1;
		h = init;
		for(i=0 ; i<n0 ; i++)
			chk0[i]=false;
		for(i=0 ; i<n1 ; i++)
			chk1[i]=false;
	}
	printf("%d",res);
}
int main()
{
	int i,x,y,z;
	scanf("%d %d",&n,&init);
	for(i=0 ; i<n ; i++)
	{
		scanf("%d %d %d",&x,&y,&z);
		if(x==0)
		{
			type0[n0].height = y;
			type0[n0].mass = z;
			n0++;
		}
		else
		{
			type1[n1].height = y;
			type1[n1].mass = z;
			n1++;
		}
	}
	sort(type0,type0+n0,compare);
	sort(type1,type1+n1,compare);
	process();
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int n,init;

struct candy {
	int height;
	int mass;
};
candy type0[2002];
candy type1[2002];
bool chk0[2002];
bool chk1[2002];

int n1,n0;
int max;

const bool compare(struct candy &l,struct candy &r)
{
	if(l.mass==r.mass)
		return(l.height>r.height);

	return(l.mass>r.mass);
}
void process()
{
	int type=0,i,j,l,flag=0;
	int h,cnt=0,res=-1;

	//0���� ����
	h = init;

	for(l=0 ; l<2 ; l++)
	{
		for(i=0 ; i<n ; i++)
		{
			flag=0;
			if(type==0)
			{
				for(j=0 ; j<n0 ; j++)
				{
					if(type0[j].height <= h && chk0[j]==false)
					{
						chk0[j]=true;
						h+=type0[j].mass;
						cnt++;
						flag=1;
						break;
					}
				}
			}
			else
			{
				for(j=0 ; j<n1 ; j++)
				{
					if(type1[j].height <= h && chk1[j]==false)
					{
						chk1[j]=true;
						h+=type1[j].mass;
						cnt++;
						flag=1;
						break;
					}
				}
			}
			if(flag==1)
				type = !type;
		}

		if(res<cnt)
			res=cnt;

		cnt=0;
		type=1;
		h = init;
		for(i=0 ; i<n0 ; i++)
			chk0[i]=false;
		for(i=0 ; i<n1 ; i++)
			chk1[i]=false;
	}
	printf("%d",res);
}
int main()
{
	int i,x,y,z;
	scanf("%d %d",&n,&init);
	for(i=0 ; i<n ; i++)
	{
		scanf("%d %d %d",&x,&y,&z);
		if(x==0)
		{
			type0[n0].height = y;
			type0[n0].mass = z;
			n0++;
		}
		else
		{
			type1[n1].height = y;
			type1[n1].mass = z;
			n1++;
		}
	}
	sort(type0,type0+n0,compare);
	sort(type1,type1+n1,compare);
	process();
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int n,init;

struct candy {
	int height;
	int mass;
};
candy type0[2002];
candy type1[2002];
bool chk0[2002];
bool chk1[2002];

int n1,n0;
int max;

const bool compare(struct candy &l,struct candy &r)
{
	if(l.height==r.height)
		return(l.mass>r.mass);

	return(l.height>r.height);
}
void process()
{
	int type=0,i,j,l;
	int h,cnt=0,res=-1;

	//0���� ����
	h = init;

	for(l=0 ; l<2 ; l++)
	{
		for(i=0 ; i<n ; i++)
		{
			if(type==0)
			{
				for(j=0 ; j<n0 ; j++)
				{
					if(type0[j].height <= h && chk0[j]==false)
					{
						chk0[j]=true;
						h+=type0[j].mass;
						cnt++;
						break;
					}
				}
			}
			else
			{
				for(j=0 ; j<n1 ; j++)
				{
					if(type1[j].height <= h && chk1[j]==false)
					{
						chk1[j]=true;
						h+=type1[j].mass;
						cnt++;
						break;
					}
				}
			}
			type = !type;
		}

		if(res<cnt)
			res=cnt;

		cnt=0;
		type=1;
		h = init;
		for(i=0 ; i<n0 ; i++)
			chk0[j]=false;
		for(i=0 ; i<n1 ; i++)
			chk1[j]=false;
	}
	printf("%d",res);
}
int main()
{
	int i,x,y,z;
	scanf("%d %d",&n,&init);
	for(i=0 ; i<n ; i++)
	{
		scanf("%d %d %d",&x,&y,&z);
		if(x==0)
		{
			type0[n0].height = y;
			type0[n0].mass = z;
			n0++;
		}
		else
		{
			type1[n1].height = y;
			type1[n1].mass = z;
			n1++;
		}
	}
	sort(type0,type0+n0,compare);
	sort(type1,type1+n1,compare);
	process();
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int n,init;

struct candy {
	int height;
	int mass;
};
candy type0[2002];
candy type1[2002];
bool chk0[2002];
bool chk1[2002];

int n1,n0;
int max;

const bool compare(struct candy &l,struct candy &r)
{
	if(l.mass==r.mass)
		return(l.height>r.height);

	return(l.mass>r.mass);
}
void process()
{
	int type=0,i,j,l;
	int h,cnt=0,res=-1;

	//0���� ����
	h = init;

	for(l=0 ; l<2 ; l++)
	{
		for(i=0 ; i<n ; i++)
		{
			if(type==0)
			{
				for(j=0 ; j<n0 ; j++)
				{
					if(type0[j].height <= h && chk0[j]==false)
					{
						chk0[j]=true;
						h+=type0[j].mass;
						cnt++;
						break;
					}
				}
			}
			else
			{
				for(j=0 ; j<n1 ; j++)
				{
					if(type1[j].height <= h && chk1[j]==false)
					{
						chk1[j]=true;
						h+=type1[j].mass;
						cnt++;
						break;
					}
				}
			}
			type = !type;
		}

		if(res<cnt)
			res=cnt;

		cnt=0;
		type=1;
		h = init;
		for(i=0 ; i<n0 ; i++)
			chk0[j]=false;
		for(i=0 ; i<n1 ; i++)
			chk1[j]=false;
	}
	printf("%d",res);
}
int main()
{
	int i,x,y,z;
	scanf("%d %d",&n,&init);
	for(i=0 ; i<n ; i++)
	{
		scanf("%d %d %d ",&x,&y,&z);
		if(x==0)
		{
			type0[n0].height = y;
			type0[n0].mass = z;
			n0++;
		}
		else
		{
			type1[n1].height = y;
			type1[n1].mass = z;
			n1++;
		}
	}
	sort(type0,type0+n0,compare);
	sort(type1,type1+n1,compare);
	process();
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct candy
{
    int h, m;
    bool operator < (candy c) const
    {
        return h < c.h;
    }
    candy(int _h, int _m) : h(_h), m(_m) {}
};

vector<candy> candies_start[2];

int main()
{
    candies_start[0].reserve(2000);
    candies_start[1].reserve(2000);

    int n, x;

    cin >> n >> x;

    for (int i = 0, t, h, m; i < n; i++)
    {
        cin >> t >> h >> m;
        candies_start[t].push_back(candy(h, m));
    }

    sort(candies_start[0].begin(), candies_start[0].end());
    sort(candies_start[1].begin(), candies_start[1].end());

    int answer = 0;

    for (int t = 0; t < 2; t++)
    {
        bool next_type = t;
        int max_jump = x;

        vector<candy> candies_current[2];
        candies_current[0] = candies_start[0];
        candies_current[1] = candies_start[1];

        int count = 0;

        while (true)
        {
            for (auto it = candies_current[next_type].begin(); it != candies_current[next_type].end(); it++)
            {
                if (it->h > max_jump)
                {
                    goto end;
                }

                max_jump += it->m;
                candies_current[next_type].erase(it);
                next_type = !next_type;
                goto next;
            }

            break;

            next:
            count++;
        }

        end:
        answer = max(answer, count);
    }

    cout << answer << endl;

    return 0;
}