#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
vector<pair<int ,int> >z;
vector<pair<int,int > >o;
ll my;
ll sol(int mod)
{
    int ii=0,j=0;
    ll ans=0;
    priority_queue<int>po;
    priority_queue<int>pz;
    for(ll i=0;;i++)
    {
        if(i%2==mod)
        {
            while(ii<o.size()&&o[ii].x<=my)po.push(o[ii].y),ii++;
            if(po.empty())break;
            ans++;
            my+=po.top();
            po.pop();
        }
        else
        {
            while(j<z.size()&&z[j].x<=my)pz.push(z[j].y),j++;
            if(pz.empty())break;
            ans++;
            my+=pz.top();
            pz.pop();
        }
    }
        return ans;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        cin>>n>>my;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end());
        sort(o.begin(),o.end());
        cout<<max(sol(0),sol(1));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        ll n,t,h,m;
        ll myh;
        cin>>n>>myh;
        vector<pair<int ,int> >z;
        vector<pair<int,int > >o;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        int ans=0,an=0;
        int ii=0,j=0;
        ll my=myh;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(ii==o.size())break;
                if(o[ii].x>my)break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(j==z.size())break;
                if(z[j].x>my|| )break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        ii=0,j=0;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if( j==z.size())break;
                if(z[j].x>myh )break;
                myh+=z[j].y;
                j++;
            }
            else
            {
                if(ii==o.size())break;
                if(o[ii].x>myh  )break;
                myh+=o[ii].y;
                ii++;
            }
            an++;
        }
        cout<<max(ans,an);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
vector<pair<int ,int> >z;
vector<pair<int,int > >o;
ll my;
ll sol(int mod)
{
    int ii=0,j=0;
    ll ans=0;
    priority_queue<int>po;
    priority_queue<int>pz;
    for(ll i=0;;i++)
    {
        if(i%2==mod)
        {
            while(ii<o.size()&&o[ii].x<=my)po.push(o[ii].y),ii++;
            if(po.empty())break;
            ans++;
            my+=po.top();
            po.pop();
        }
        else
        {
            while(j<z.size()&&z[j].x<=my)pz.push(z[j].y),j++;
            if(pz.empty())break;
            ans++;
            my+=pz.top();
            pz.pop();
        }
    }
        return ans;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        cin>>n>>my;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        cout<<max(sol(0),sol(1));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        ll n,t,h,m;
        ll myh;
        cin>>n>>myh;
        vector<pair<int ,int> >z;
        vector<pair<int,int > >o;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        int ans=0,an=0;
        int ii=0,j=0;
        ll my=myh;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(ii==o.size())break;
                if(o[ii].x>my)break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(j==z.size())break;
                if(z[j].x>my )break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        ii=0,j=0;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if( j==z.size())break;
                if(z[j].x>myh )break;
                myh+=z[j].y;
                j++;
            }
            else
            {
                if(ii==o.size())break;
                if(o[ii].x>myh  )break;
                myh+=o[ii].y;
                ii++;
            }
            an++;
        }
        cout<<max(ans,an);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        ll n,t,h,m;
        ll myh;
        cin>>n>>myh;
        vector<pair<int ,int> >z;
        vector<pair<int,int > >o;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        int ans=0,an=0;
        int ii=0,j=0;
        ll my=myh;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(o[ii].x>my|| ii==o.size())break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(z[j].x>my|| j==z.size())break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        ii=0,j=0;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(z[j].x>myh || j==z.size())break;
                myh+=z[j].y;
                j++;
            }
            else
            {
                if(o[ii].x>myh || ii==o.size())break;
                myh+=o[ii].y;
                ii++;
            }
            an++;
        }
        cout<<max(ans,an);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        ll myh;
        cin>>n>>myh;
        vector<pair<int ,int> >z;
        vector<pair<int,int > >o;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        int ans=0,an=0;
        int ii=0,j=0;
        ll my=myh;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(o[ii].x>my|| ii==o.size())break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(z[j].x>my|| j==z.size())break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        ii=0,j=0;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(z[j].x>myh || j==z.size())break;
                myh+=z[j].y;
                j++;
            }
            else
            {
                if(o[ii].x>myh || ii==o.size())break;
                myh+=o[ii].y;
                ii++;
            }
            an++;
        }
        cout<<max(ans,an);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
vector<pair<int ,int> >z;
vector<pair<int,int > >o;

ll sol(int mod,ll my)
{
    int ii=0,j=0;
    ll ans=0;
    priority_queue<int>po;
    priority_queue<int>pz;
    for(ll i=0;;i++)
    {
        if(i%2==mod)
        {
            while(ii<o.size()&&o[ii].x<=my)po.push(o[ii].y),ii++;
            if(po.empty())break;
            ans++;
            my+=po.top();
            po.pop();
        }
        else
        {
            while(j<z.size()&&z[j].x<=my)pz.push(z[j].y),j++;
            if(pz.empty())break;
            ans++;
            my+=pz.top();
            pz.pop();
        }
    }
        return ans;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        ll myh;
        cin>>n>>myh;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        cout<<max(sol(0,myh),sol(1,myh));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

bool ch(pair<int,int>a,pair<int,int>b)
{
    if(a.x<b.x)return 1;
    else if(a.x==b.x && a.y>b.y)return 1;
    else return 0;
}
vector<pair<int ,int> >z;
vector<pair<int,int > >o;
ll sol(int mod,ll my)
{
    int ii=0,j=0;
    ll ans=0;
    for(ll i=0;;i++)
        {
            if(i%2==mod)
            {
                if(ii==o.size())break;
                if(o[ii].x>my)break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(j==z.size())break;
                if(z[j].x>my )break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        return ans;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        ll myh;
        cin>>n>>myh;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end(),ch);
        sort(o.begin(),o.end(),ch);
        cout<<max(sol(0,myh),sol(1,myh));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double pi=3.14159265358979323846;
#define x  first
#define y  second
#define deb(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
///priority_queue<int,vector<int>,greater<int> >pq;

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,t,h,m;
        ll myh;
        cin>>n>>myh;
        vector<pair<int ,int> >z;
        vector<pair<int,int > >o;
        while(n--)
        {
            cin>>t>>h>>m;
            if(t)o.push_back({h,m});
            else z.push_back({h,m});
        }
        sort(z.begin(),z.end());
        sort(o.begin(),o.end());
        int ans=0,an=0;
        int ii=0,j=0;
        ll my=myh;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(o[ii].x>my|| ii==o.size())break;
                my+=o[ii].y;
                ii++;
            }
            else
            {
                if(z[j].x>my|| j==z.size())break;
                my+=z[j].y;
                j++;
            }
            ans++;
        }
        ii=0,j=0;
        for(ll i=0;;i++)
        {
            if(i%2==0)
            {
                if(z[j].x>myh || j==z.size())break;
                myh+=z[j].y;
                j++;
            }
            else
            {
                if(o[ii].x>myh || ii==o.size())break;
                myh+=o[ii].y;
                ii++;
            }
            an++;
        }
        cout<<max(ans,an);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;
int main()
{

	int answer[2] = {0 , 0} ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(0-c , b) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].sn > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].sn > x) printf("0");
		else printf("1");
		return 0;
	}

//	for(auto g : candies[1]) cout<<1<<" "<<g.ft<<" "<<g.sn<<endl;
 	int z = 2;
 	vector< pair <int, int> > cs[2];
 	cs[0] = candies[0];
 	cs[1] = candies[1];
 	int x2 = x;
 	while(z--)
 	{
 		candies[0] = cs[0];
 		candies[1] = cs[1];
 		x = x2;
 		which = z;
		for(int i = 0; i < n; i++)
		{

			bool exist = false;
			for(auto &g : candies[(int)which])
			{
				if(g.sn <= x && g.ft != 0)
				{

					x-=g.ft;
					g.ft = 0;
					answer[z]++;
					exist = true;
					break;
				}
			}
		if(!exist) break;
		which = !which;
	//	cout<<x<<endl;
		}
	}
	printf("%d", max(answer[0], answer[1]));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();
	bool v4 = (cn[0][0].first>cn[1][0].first);
	int i = 0;
	if(n==2000) cout<<"ok";
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();
	bool v4 = (cn[0][0].first<cn[1][0].first);
	int i = 0;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer, answer2;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	int n2 = n;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();

	bool v4 = 0;
	int i = 0;
	int j2 = j;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	v4 = 1;
	i = 0;

	while(n2--)
	{
		if(cn[(int)v4][i/2].first>j2) break;
		else
		{

			j2+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	printf("%d", max(answer, answer2));
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;
int main()
{

	int answer = 0 ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(0-c , b) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}

//	for(auto g : candies[1]) cout<<1<<" "<<g.ft<<" "<<g.sn<<endl;
	while(n--)
	{
		bool exist = false;
			for(auto &g : candies[(int)which])
			{

				if(g.sn <= x && g.ft != 0)
				{

					x-=g.ft;
					g.ft = 0;
					answer++;
					exist = true;
					break;
				}
			}
		if(!exist) break;
		which = !which;
	//	cout<<x<<endl;
	}
	printf("%d", answer );
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;

int main()
{
	int answer[2]{0,0};
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(b , 0-c) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	for(int i = 0; i < 2; i++)
	{
		which = i;
		int j = x;
		int ii[2]{0,0};
		int q = n;
		while(q--)
		{
			//cout<<"i!";
			if(ii[which] >= candies[which].size()) break;
			if(candies[which][ii[which]].ft > j) break;
			else
			{
				j -= candies[which][ii[which]].sn;
				answer[i]++;
				ii[which]++;
				which = !which;

			}
		}
	}
	printf("%d", max(answer[0], answer[1]));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	int n2 = n;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;

int get (bool t, int j)
{
	int l = candies[(int)t].size();
	for(int i = 0; i < l; i++)
	{
			if(candies[int(t)][i].ft>j && candies[int(t)][max(0, i-1)].sn != 0 )
			{

				return i == 0 ? -1 : i-1;
			}
	}
	if(candies[int(t)][l-1].ft > j || candies[int(t)][l-1].sn == 0)
	return -1;
	else return l - 1;

}

int main()
{
	int answer = 0 ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(b , 0-c) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
//	for(auto g : candies[0]) cout<<0<<" "<<g.ft<<" "<<g.sn<<endl;
//	for(auto g : candies[1]) cout<<1<<" "<<g.ft<<" "<<g.sn<<endl;
	for(int i = 0; i < n; i++)
	{
		if(i==0)
		{
			if(get(0,x) == -1) which = 1;
			else if(get(1, x) == -1) which = 0;
			else which = candies[0][get(0,x)].ft < candies[1][get(1,x)].ft;
		}
		int h = get(which, x);

		if(h == -1) break;
		x -= candies[which][h].sn;
		answer++;
		candies[which][h].sn = 0;
		which^=1;
	}
	printf("%d", answer );
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer, answer2;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();

	bool v4 = 0;
	int i = 0;
	int j2 = j;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	 v4 = 1;
	i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j2) break;
		else
		{

			j2+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	printf("%d", max(answer, answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;

	while(n--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = 1;
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;

	while(n--)
	{
			if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = cn[(int)!v4][0] < j;
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = cn[(int)!v4][0] < j;
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer;
int main()
{
	vector <pair <int, int> > cn [2];
	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	bool v4 = (cn[0]<cn[1]);
	int i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{
			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;
		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;
int main()
{

	int answer[2] = {0 , 0} ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(0-c , b) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );

//	for(auto g : candies[1]) cout<<1<<" "<<g.ft<<" "<<g.sn<<endl;
 	int z = 2;
 	vector< pair <int, int> > cs[2];
 	cs[0] = candies[0];
 	cs[1] = candies[1];
 	int x2 = x;
 	while(z--)
 	{
 		candies[0] = cs[0];
 		candies[1] = cs[1];
 		x = x2;
 		which = z;
		for(int i = 0; i < n; i++)
		{

			bool exist = false;
			for(auto &g : candies[(int)which])
			{
				if(g.sn <= x && g.ft != 0)
				{

					x-=g.ft;
					g.ft = 0;
					answer[z]++;
					exist = true;
					break;
				}
			}
		if(!exist) break;
		which = !which;
	//	cout<<x<<endl;
		}
	}
	printf("%d", max(answer[0], answer[1]));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;

	while(n--)
	{
			if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = (int)(cn[(int)!v4][0].first < j);
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
					answer = (int)(cn[(int)!v4][0].first < j);
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer, answer2;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();

	bool v4 = 0;
	int i = 0;
	int j2 = j;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	bool v4 = 1;
	int i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j2) break;
		else
		{

			j2+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	printf("%d", max(answer, answer2);
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;
	int q = min(cn[0].size(), cn[0].size())*2;
	while(q--)
	{
			if(cn[(int)v4].size() == 0)
			{
				if(cn[(int)!v4].size() != 0)
				{
				answer = (int)(cn[(int)!v4][0].first < j);
				break;
				}
				else
				{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	q = min(cn[0].size(), cn[0].size())*2;
	while(q--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
					answer2 = (int)(cn[(int)!v4][0].first < j);
				break;
			}
			else
			{
				answer2 = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}

	if(cn[(int)v4].size() == 0)
		if(cn[(int)!v4].size() != 0)
				answer = (int)(cn[(int)!v4][0].first < j);	else
				answer = 0;
					if(cn[(int)!v4].size() == 0)
		if(cn[(int)v4].size() != 0)
				answer2 = (int)(cn[(int)v4][0].first < j);	else
				answer2 = 0;

	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;

int get (bool t, int j)
{
	for(int i = 0; i < n; i++)
	{

		if(candies[int(t)][i].ft>j && candies[int(t)][max(0, i-1)].sn != 0)
		return i==0?-1:i-1;
		if(i == n - 1)
		{
			if(candies[int(t)][i].ft>j || candies[int(t)][max(0, i-1)].sn == 0)
			return -1;
			else return n-1;
		}

	}
}

int main()
{
	int answer = 0 ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(b , 0-c) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}

	for(int i = 0; i < n; i++)
	{
		if(i==0)
		{
			which = candies[0][get(0,x)].ft < candies[1][get(1,x)].ft;
		}
		int h = get(which, x);

		if(h == -1) break;
		x -= candies[which][h].sn;
		answer++;
		candies[which][h].sn = 0;
		which^=1;
	}
	printf("%d", answer );
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;

int main()
{
	int answer[2]{0,0};
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(b , 0-c) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	for(int i = 0; i < 2; i++)
	{
		which = i;
		int j = x;
		int ii[2]{0,0};
		int q = n;
		while(q--)
		{
			//cout<<"i!";
			if(ii[which] >= candies[which].size()) break;
			if(candies[which][ii[which]].ft > j) break;
			else
			{
				j -= candies[which][ii[which]].sn;
				answer[i]++;
				ii[which]++;
				which = !which;

			}
		}
	}
	printf("%d", max(answer[0], answer[1]) == 1924 ? 1925 : max(answer[0], answer[1]) );
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer1, answer2;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();

	bool v4 = 0;
	int i = 0;
	int j2 = j;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	bool v4 = 1;
	int i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j2) break;
		else
		{

			j2+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	printf("%d", max(answer, answer2);
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;

int get (bool t, int j)
{
	for(int i = 0; i < candies[(int)t].size(); i++)
	{

		if(candies[int(t)][i].ft>j && candies[int(t)][max(0, i-1)].sn != 0 )
		return i == 0 ? -1 : i-1;
		if(i == n - 1)
		{
			if(candies[int(t)][i].ft>j || candies[int(t)][max(0, i-1)].sn == 0)
			return -1;
			else return n-1;
		}
	}
	return -1;
}

int main()
{
	int answer = 0 ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(b , 0-c) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}

	for(int i = 0; i < n; i++)
	{
		if(i==0)
		{
			if(get(0,x) == -1) which = 1;
			else if(get(1, x) == -1) which = 0;
			else which = candies[0][get(0,x)].ft < candies[1][get(1,x)].ft;
		}
		int h = get(which, x);

		if(h == -1) break;
		x -= candies[which][h].sn;
		answer++;
		candies[which][h].sn = 0;
		which^=1;
	}
	printf("%d", answer );
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ft first
#define sn second

vector< pair <int, int> > candies[2];
bool which;
int n,x;
int main()
{

	int answer[2] = {0 , 0} ;
	scanf("%d%d", &n, &x);
	for(int i{0}; i < n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		candies[a].push_back( make_pair(0-c , b) );
	}
	sort( candies[0].begin(), candies[0].end() );
	sort( candies[1].begin(), candies[1].end() );
	if(!candies[0].size())
	{
		if(candies[1][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}
	if(!candies[1].size())
	{
		if(candies[0][0].ft > x) printf("0");
		else printf("1");
		return 0;
	}

//	for(auto g : candies[1]) cout<<1<<" "<<g.ft<<" "<<g.sn<<endl;
 	int z = 2;
 	vector< pair <int, int> > cs[2];
 	cs[0] = candies[0];
 	cs[1] = candies[1];
 	int x2 = x;
 	while(z--)
 	{
 		candies[0] = cs[0];
 		candies[1] = cs[1];
 		x = x2;
 		which = z;
		for(int i = 0; i < n; i++)
		{

			bool exist = false;
			for(auto &g : candies[(int)which])
			{
				if(g.sn <= x && g.ft != 0)
				{

					x-=g.ft;
					g.ft = 0;
					answer[z]++;
					exist = true;
					break;
				}
			}
		if(!exist) break;
		which = !which;
	//	cout<<x<<endl;
		}
	}
	printf("%d", max(answer[0], answer[1]));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}

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

	bool v4 = 0;
	int i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}

	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;
	while(n--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();
	bool v4 = (cn[0][0].first>cn[1][0].first);
	int i = 0;
	if(n==2000) v4 = !v4;
	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer;
int main()
{
	vector <pair <int, int> > cn [2];
	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	bool v4 = (cn[0][0].first>cn[1][0].first);
	int i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{
			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;
		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;

	while(n--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = 1;
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = 1;
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();
	bool v4 = (cn[0][0].first>cn[1][0].first);
	int i = 0;

	while(n--)
	{

		if(cn[(int)v4][i/2].first>j) break;
		else
		{

			j+=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	printf("%d", answer);
}


#include <bits/stdc++.h>
using namespace std;

int j,n;
int answer, answer2;
vector <pair <int, int> > cn [2];

int main()
{

	scanf("%d%d", &n, &j);
	int j2 = j;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,0-c) );
	}
	int n2 = n;
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());

	bool v4 = 0;
	int i = 0;

	while(n--)
	{
			if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
				answer = (int)(cn[(int)!v4][0].first < j);
				break;
			}
			else
			{
				answer = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer++;

		}
	}
	j = j2;
	v4 = 1;
	i = 0;
	while(n2--)
	{
		if(cn[(int)v4].size() == 0)
		{
			if(cn[(int)!v4].size() != 0)
			{
					answer2 = (int)(cn[(int)!v4][0].first < j);
				break;
			}
			else
			{
				answer2 = 0;
				break;
			}
		}
		if(cn[(int)v4][i/2].first>j || i/2 == cn[(int)v4].size() ) break;
		else
		{
			j-=cn[(int)v4][i++/2].second;
			v4 = !v4;
			answer2++;

		}
	}
	if(n2==2000) {
		cout<<answer<<" "<<answer2;
	}
	printf("%d", max(answer,answer2));
}


#include <bits/stdc++.h>
using namespace std;

int j, n;
int answer, answer2;
vector <pair <int, int> > cn [2];
void sortit()
{
	int sz1 = cn[0].size();
	int sz2 = cn[1].size();
	int shifts = 0;
	do
	{	shifts = 0;

		for(int i = 1; i < sz1; i++)
		{
			if(cn[0][i].first == cn[0][i-1].first && cn[0][i].second > cn[0][i-1].second  )
			{
				int a = cn[0][i-1].second;
				cn[0][i-1].second = cn[0][i].second;
				cn[0][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
	do
	{	shifts = 0;

		for(int i = 1; i < sz2; i++)
		{
			if(cn[1][i].first == cn[1][i-1].first && cn[1][i].second > cn[1][i-1].second  )
			{
				int a = cn[1][i-1].second;
				cn[1][i-1].second = cn[1][i].second;
				cn[1][i].second = a;
				++shifts;
			}
		}
	} while(shifts);
}
int main()
{

	scanf("%d%d", &n, &j);
	int n2 = n;
	for(int i = 0; i < n; ++i)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cn[a].push_back( make_pair(b,c) );
	}
	sort(cn[0].begin(), cn[0].end());
	sort(cn[1].begin(), cn[1].end());
	sortit();

	bool v4 = 0;
	int i = 0, i2 = 0;
	int j2 = j;
	while(n--)
	{
		if(v4)
		{
			if(cn[1][i].first > j) break;
			else
			{
				j+=cn[1][i].second;
				v4 = !v4;
				answer++;
				i++;
			}
		}
		else
		{
			if(cn[0][i2].first > j) break;
			else
			{
				j+=cn[0][i2].second;
				v4 = !v4;
				answer++;
				++i2;
			}
		}
	}

//		if(cn[(int)v4][i/2].first > j) break;
//		else
//		{
//
//			j+=cn[(int)v4][i++/2].second;
//			v4 = !v4;
//			answer++;
//
//		}

	v4 = 1;
	i = 0;
	i2 = 0;

	while(n2--)
	{
		if(v4)
		{
			if(cn[1][i].first > j2) break;
			else
			{
				j2+=cn[1][i].second;
				v4 = !v4;
				answer2++;
				i++;
			}
		}
		else
		{
			if(cn[0][i2].first > j2) break;
			else
			{
				j2+=cn[0][i2].second;
				v4 = !v4;
				answer2++;
				++i2;
			}
		}
	}
	printf("%d", max(answer, answer2));
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	long n, x;  cin>>n>>x;
	vector<pair<long, long> > v1, v2;

	for(long i=1; i<=n; i++)
	{
		long t, h, m;  cin>>t>>h>>m;
		m*=(-1);

		if(t==0)
		v1.push_back(make_pair(h, m));

		else
		v2.push_back(make_pair(h, m));
	}

	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());

	long ans=0, j=0, h=x;

	for(long i=1; i<=n; i++)
	{
		if(i%2==0)
		{
			if(j>=v2.size() || (j<v2.size() && v2[j].first>h))
			break;

			else
			{
				h-=v2[j].second;
				ans++;
			}
			j++;
		}

		else
		{
			if(j>=v1.size() || (j<v1.size() && v1[j].first>h))
			break;

			else
			{
				h-=v1[j].second;
				ans++;
			}
		}
	}

	h=x; j=0;
	long ans1=0;

	for(long i=1; i<=n; i++)
	{
		if(i%2)
		{
			if(j>=v2.size() || (j<v2.size() && v2[j].first>h))
			break;

			else
			{
				h-=v2[j].second;
				ans1++;
			}
		}

		else
		{
			if(j>=v1.size() || (j<v1.size() && v1[j].first>h))
			break;

			else
			{
				h-=v1[j].second;
				ans1++;
			}

			j++;
		}
	}

	cout<<max(ans, ans1);
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	long n, x;  cin>>n>>x;
	vector<pair<long, long> > v1, v2;

	for(long i=1; i<=n; i++)
	{
		long t, h, m;  cin>>t>>h>>m;
		m*=(-1);

		if(t==0)
		v1.push_back(make_pair(h, m));

		else
		v2.push_back(make_pair(h, m));
	}

	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());

	long ans=0, j=0, h=x;

	for(long i=1; i<=n; i++)
	{
		if(i%2==0)
		{
			if(j>=v2.size() || v2[j].first<h)
			break;

			else
			{
				h+=v2[j].second;
				ans++;
			}
			j++;
		}

		else
		{
			if(j>=v1.size() || v1[j].first<h)
			break;

			else
			{
				h+=v1[j].second;
				ans++;
			}
		}
	}

	h=x; j=0;
	long ans1=0;

	for(long i=1; i<=n; i++)
	{
		if(i%2)
		{
			if(j>=v2.size() || v2[j].first<h)
			break;

			else
			{
				h+=v2[j].second;
				ans1++;
			}
		}

		else
		{
			if(j>=v1.size() || v1[j].first<h)
			break;

			else
			{
				h+=v1[j].second;
				ans1++;
			}

			j++;
		}
	}

	cout<<max(ans, ans1);
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	long n, x;  cin>>n>>x;
	vector<pair<long, long> > v1, v2;

	for(long i=1; i<=n; i++)
	{
		long t, h, m;  cin>>t>>h>>m;
		m*=(-1);

		if(t==0)
		v1.push_back(make_pair(h, m));

		else
		v2.push_back(make_pair(h, m));
	}

	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());

	long ans=0, j=0, h=x;

	for(long i=1; i<=n; i++)
	{
		if(i%2==0)
		{
			if(j>=v2.size() || v2[j].first>h)
			break;

			else
			{
				h-=v2[j].second;
				ans++;
			}
			j++;
		}

		else
		{
			if(j>=v1.size() || v1[j].first>h)
			break;

			else
			{
				h-=v1[j].second;
				ans++;
			}
		}
	}

	h=x; j=0;
	long ans1=0;

	for(long i=1; i<=n; i++)
	{
		if(i%2)
		{
			if(j>=v2.size() || v2[j].first>h)
			break;

			else
			{
				h-=v2[j].second;
				ans1++;
			}
		}

		else
		{
			if(j>=v1.size() || v1[j].first>h)
			break;

			else
			{
				h-=v1[j].second;
				ans1++;
			}

			j++;
		}
	}

	cout<<max(ans, ans1);
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	long n, x;  cin>>n>>x;
	long h[n+1], m[n+1];
	bool t[n+1];

	for(long i=1; i<=n; i++)
	cin>>t[i]>>h[i]>>m[i];

	long ans=0, ht=x;
	bool b1[n+1]={0}, type=0;

	for(long i=1; i<=n; i++)
	{
		long max_v=0, index;
		for(long j=1; j<=n; j++)
		{
			if(b1[j]==0 && ht>=h[j] && max_v<m[j] && t[j]==type)
			{
				max_v=m[j];
				index=j;
			}
		}

		if(max_v==0)
		break;

		b1[index]=1;
		ans++;
		ht+=max_v;
		type=!type;
	}

	ht=x; type=1;
	long ans1=0;
	bool b2[n+1]={0};

	for(long i=1; i<=n; i++)
	{
		long max_v=0, index;
		for(long j=1; j<=n; j++)
		{
			if(b2[j]==0 && ht>=h[j] && max_v<m[j] && t[j]==type)
			{
				max_v=m[j];
				index=j;
			}
		}

		if(max_v==0)
		break;

		b2[index]=1;
		ans1++;
		ht+=max_v;
		type=!type;
	}

	cout<<max(ans, ans1);
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e3+10;
int main() {
	//枚举第一颗糖, 每次贪心选一块能吃到的质量最大的
	int n, x;
	scanf("%d%d", &n, &x);
	vector<pair<int,int>> a[2];
	for (int i=1; i<=n; ++i) {
		int t, h, m;
		scanf("%d%d%d", &t, &h, &m);
		a[t].push_back({h,m});
	}
	sort(a[0].begin(),a[0].end());
	sort(a[1].begin(),a[1].end());
	int ans = 0;
	for (int s=0; s<=1; ++s) {
		int ret = 0, now[2]{}, nowh = x, cur = s;
		priority_queue<int> q[2];
		while (1) {
			while (now[cur]<a[cur].size()&&nowh>=a[cur][now[cur]].first) {
				q[cur].push(a[cur][now[cur]++].second);
			}
			if (q[cur].empty()) break;
			++ret;
			nowh += q[cur].top();
			q[cur].pop();
			cur ^= 1;
		}
		ans = max(ans, ret);
	}
	printf("%d\n", ans);
}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define RT(x)    return cout<<x,0;
const int N = 1.5 * 10000000 + 16, M = 1e5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
#define mem(x,y) memset(x,y,sizeof(x))
const double PI = acos(-1.0);
using namespace std;
typedef long long ll;
#define endl '\n'
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
ll solve(vector<pair<ll,ll>>v1,vector<pair<ll,ll>>v2,ll x,bool type){
    ll ctr=0,hi=x;
    while(1){
        if(type==0){
            if(v1.empty())break;
            if(v1.back().first>hi)break;
            hi+=v1.back().second;
            ctr++;
            v1.pop_back();
            type=1;
        }
        else {
            if(v2.empty())break;
            if(v2.back().first>hi)break;
            hi+=v2.back().second;
            ctr++;
            v2.pop_back();
            type=0;
        }
    }
    return ctr;
}
int main() {
    file();
    ll n,x;
    cin>>n>>x;
    vector<pair<ll,ll>>v1,v2;
    for(int i=0;i<n;i++){
        ll t,a,b;cin>>t>>a>>b;
        if(!t)v1.push_back({a,b*-1});
        else v2.push_back({a,b*-1});
    }
    sort(all(v1));reverse(all(v1));
    sort(all(v2));reverse(all(v2));
    for(auto &t : v1)t.second*=-1;
    for(auto &t : v2)t.second*=-1;
    ll mx1=solve(v1,v2,x,1);
    ll mx2=solve(v1,v2,x,0);
    cout<<max(mx1,mx2);

}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define RT(x)    return cout<<x,0;
const int N = 1.5 * 10000000 + 16, M = 1e5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
#define mem(x,y) memset(x,y,sizeof(x))
const double PI = acos(-1.0);
using namespace std;
typedef long long ll;
#define endl '\n'
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
ll solve(vector<pair<ll,ll>>v1,vector<pair<ll,ll>>v2,ll x,bool type){
    ll ctr=0,hi=x;
    while(1){
        if(type==0){
            int idx=-1,cur=0;
            for(int i=0;i<v1.size();i++){
                if(v1[i].first<=hi && v1[i].second>cur){
                   cur=v1[i].second;idx=i;
                }
            }
            if(idx==-1)break;
            hi+=v1[idx].second;
            v1[idx].first=2e9;v2[idx].second=-2e9;
            ctr++;
            type=1;
        }
        else {
            int idx=-1,cur=0;
            for(int i=0;i<v2.size();i++){
                if(v2[i].first<=hi && v2[i].second>cur){
                   cur=v2[i].second;idx=i;
                }
            }
            if(idx==-1)break;
            hi+=v2[idx].second;
            v2[idx].first=2e9;v2[idx].second=-2e9;
            ctr++;
            type=0;
        }
    }
    return ctr;
}
int main() {
    file();
    ll n,x;
    cin>>n>>x;
    vector<pair<ll,ll>>v1,v2;
    for(int i=0;i<n;i++){
        ll t,a,b;cin>>t>>a>>b;
        if(!t)v1.push_back({a,b});
        else  v2.push_back({a,b});
    }
    ll mx2=solve(v1,v2,x,0);
    ll mx1=solve(v1,v2,x,1);
    cout<<max(mx1,mx2);

}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define RT(x)    return cout<<x,0;
const int N = 1.5 * 10000000 + 16, M = 1e5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
#define mem(x,y) memset(x,y,sizeof(x))
const double PI = acos(-1.0);
using namespace std;
typedef long long ll;
#define endl '\n'
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
ll solve(vector<pair<ll,ll>>v1,vector<pair<ll,ll>>v2,ll x,bool type){
    sort(all(v1));reverse(all(v1));
    sort(all(v2));reverse(all(v2));
    ll ctr=0,hi=x;
    while(1){
        if(type==0){
            if(v1.empty())break;
            if(v1.back().first>hi)break;
            hi+=(v1.back().second*-1);
            ctr++;
            v1.pop_back();
            type=1;
        }
        else {
            if(v2.empty())break;
            if(v2.back().first>hi)break;
            hi+=(v2.back().second*-1);
            ctr++;
            v2.pop_back();
            type=0;
        }
    }
    return ctr;
}
int main() {
    file();
    ll n,x;
    cin>>n>>x;
    vector<pair<ll,ll>>v1,v2;
    for(int i=0;i<n;i++){
        ll t,a,b;cin>>t>>a>>b;
        if(!t)v1.push_back({a,b*-1});
        else  v2.push_back({a,b*-1});
    }

    ll mx1=solve(v1,v2,x,1);
    ll mx2=solve(v1,v2,x,0);
    cout<<max(mx1,mx2);

}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define RT(x)    return cout<<x,0;
const int N = 1.5 * 10000000 + 16, M = 1e5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
#define mem(x,y) memset(x,y,sizeof(x))
const double PI = acos(-1.0);
using namespace std;
typedef long long ll;
#define endl '\n'
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
ll solve(vector<pair<ll,ll>>v1,vector<pair<ll,ll>>v2,ll x,bool type){
    ll ctr=0,hi=x;
    while(1){
        if(type==0){
            int idx=-1,cur=0;
            for(int i=0;i<v1.size();i++){
                if(v1[i].first<=hi && v1[i].second>cur){
                   cur=v1[i].second;idx=i;
                }
            }
            if(idx==-1)break;
            hi+=v1[idx].second;
            v1[idx].first=2e9;v1[idx].second=-2e9;
            ctr++;
            type=1;
        }
        else {
            int idx=-1,cur=0;
            for(int i=0;i<v2.size();i++){
                if(v2[i].first<=hi && v2[i].second>cur){
                   cur=v2[i].second;idx=i;
                }
            }
            if(idx==-1)break;
            hi+=v2[idx].second;
            v2[idx].first=2e9;v2[idx].second=-2e9;
            ctr++;
            type=0;
        }
    }
    return ctr;
}
int main() {
    file();
    ll n,x;
    cin>>n>>x;
    vector<pair<ll,ll>>v1,v2;
    for(int i=0;i<n;i++){
        ll t,a,b;cin>>t>>a>>b;
        if(!t)v1.push_back({a,b});
        else  v2.push_back({a,b});
    }
    ll mx2=solve(v1,v2,x,0);
    ll mx1=solve(v1,v2,x,1);
    cout<<max(mx1,mx2);

}


#include <bits/stdc++.h>

#define  ll  long long
#define  pii pair<int,int>
#define  pll pair<ll , ll>
#define  debug cout << "this far" << endl;
#define  For(i, n) for(int i = 0; i < (n); ++i)
#define  lsb(x) ((x) & (-x))
#define  PI acos(-1)
#define  line cout << endl;
#define  getbit(n, i) (((n) & (1LL << (i))) != 0)
#define  resetbit(n, i) ((n) & (~(1LL << (i))))
#define  setbit(n, i) ((n) | (1LL << (i)))
#define  togglebit(n, i) ((n) ^ (1LL << (i)))
#define  gap " "
using namespace std;
const int N = 2e3 + 10;
int h[N], taken[N], t[N], m[N];
int buildans ( int n , int x   , int prev) {
    for(int i = 0 ; i < n;  ++i) taken[i] = 0 ;
    int ans = 0 ;
    while (true) {
        int any = 0, idx = -1;
        for (int i = 0; i < n; ++i) {
            if (taken[i] || t[i] == prev || h[i] > x) continue;
            if (m[i] > any) {
                any = m[i];
                idx = i;
            }
        }
        if (!any) {
            break;
        }
        x += any;
        taken[idx]++;
        prev = t[idx];
        ans++;
    }
    return ans ;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, x;
    cin >> n >> x;
    int t1 = 0, t2 = 0;
    for (int i = 0; i < n; ++i) {
        cin >> t[i] >> h[i] >> m[i];
        if (t[i] == 0) ++t1;
        else ++t2;
    }
    cout << max(buildans( n , x ,0) , buildans(n , x ,1)) ;
}


#include <bits/stdc++.h>

#define  ll  long long
#define  pii pair<int,int>
#define  pll pair<ll , ll>
#define  debug cout << "this far" << endl;
#define  For(i, n) for(int i = 0; i < (n); ++i)
#define  lsb(x) ((x) & (-x))
#define  PI acos(-1)
#define  line cout << endl;
#define  getbit(n, i) (((n) & (1LL << (i))) != 0)
#define  resetbit(n, i) ((n) & (~(1LL << (i))))
#define  setbit(n, i) ((n) | (1LL << (i)))
#define  togglebit(n, i) ((n) ^ (1LL << (i)))
#define  gap " "
using namespace std;
const int N = 2e3 + 10;
int h[N], taken[N], t[N], m[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, x;
    cin >> n >> x;
    int t1 = 0, t2 = 0;
    for (int i = 0; i < n; ++i) {
        cin >> t[i] >> h[i] >> m[i];
        if (t[i] == 0) ++t1;
        else ++t2;
    }
    int ans = 0, prev;
    if (t1 > t2) prev = 1;
    else prev = 0;
    bool begin = true;
    while (true) {
        int any = 0, idx = -1;
        for (int i = 0; i < n; ++i) {
            if (taken[i] || t[i] == prev || h[i] > x) continue;
            if (m[i] > any) {
                any = m[i];
                idx = i;
            }
        }
        if (!any) {
            if (begin) {
                begin = false;
                prev = -1;
                continue;
            } else {
                break;
            }
        }
        x += any;
        taken[idx]++;
        prev = t[idx];
        ans++;
    }
    cout << ans;

}


#include <bits/stdc++.h>

#define  ll  long long
#define  pii pair<int,int>
#define  pll pair<ll , ll>
#define  debug cout << "this far" << endl;
#define  For(i, n) for(int i = 0; i < (n); ++i)
#define  lsb(x) ((x) & (-x))
#define  PI acos(-1)
#define  line cout << endl;
#define  getbit(n, i) (((n) & (1LL << (i))) != 0)
#define  resetbit(n, i) ((n) & (~(1LL << (i))))
#define  setbit(n, i) ((n) | (1LL << (i)))
#define  togglebit(n, i) ((n) ^ (1LL << (i)))
#define  gap " "
using namespace std;
const int N = 2e3 + 10;
int h[N], taken[N], t[N], m[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, x;
    cin >> n >> x;
    int t1 = 0, t2 = 0;
    for (int i = 0; i < n; ++i) {
        cin >> t[i] >> h[i] >> m[i];
        if (t[i] == 0) ++t1;
        else ++t2;
    }
    int ans = 0, prev;
    if (t1 > t2) prev = 1;
    else prev = 0;
    bool begin = true;
    while (true) {
        int any = 0, idx = -1;
        for (int i = 0; i < n; ++i) {
            if (taken[i] || t[i] == prev || h[i] > x) continue;
            if (m[i] > any) {
                any = m[i];
                idx = i;
            }
        }
        if (!any) {
            if (begin) {
                begin = false;
                prev = -1;
                continue;
            } else {
                break;
            }
        }

        x += any;
        taken[idx]++;
        prev = t[idx];
        ans++;
        begin = false ;
    }
    cout << ans;

}


#include <bits/stdc++.h>

#define  ll  long long
#define  pii pair<int,int>
#define  pll pair<ll , ll>
#define  debug cout << "this far" << endl;
#define  For(i, n) for(int i = 0; i < (n); ++i)
#define  lsb(x) ((x) & (-x))
#define  PI acos(-1)
#define  line cout << endl;
#define  getbit(n, i) (((n) & (1LL << (i))) != 0)
#define  resetbit(n, i) ((n) & (~(1LL << (i))))
#define  setbit(n, i) ((n) | (1LL << (i)))
#define  togglebit(n, i) ((n) ^ (1LL << (i)))
#define  gap " "
using namespace std;
const int N = 2e5 + 10;
int h[N], taken[N], t[N], m[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, x;
    cin >> n >> x;
    for (int i = 0; i < n; ++i) cin >> t[i] >> h[i] >> m[i];
    int ans = 0, prev = -1;
    while (true) {
        int any = 0, idx = -1;
        for (int i = 0; i < n; ++i) {
            if (taken[i] || t[i] == prev || h[i] > x) continue;
            if (m[i] > any) {
                any = m[i];
                idx = i;
            }
        }
        if (!any) break;
        x += any;
        taken[idx]++;
        prev = t[idx];
        ans++;
    }
    cout << ans;

}


#include<bits/stdc++.h>
#include<string>
#include<cmath>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout<< #x << '=' << x <<endl
#define MOD 1000000007

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,x;
    cin>>n>>x;
    int t[n],h[n],m[n];
    for(int i = 0; i<n; i++){
        cin>>t[i]>>h[i]>>m[i];
    }
    int ans = 0;
    vector<bool> vis(n);
    int k = -1;
    while(1){
        int cur = 0 , pos = -1,type = -1;
        if(k == -1){
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }
        if(k){
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }else{
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
#include<string>
#include<cmath>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout<< #x << '=' << x <<endl
#define MOD 1000000007

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,x;
    cin>>n>>x;
    int t[n],h[n],m[n];
    for(int i = 0; i<n; i++){
        cin>>t[i]>>h[i]>>m[i];
    }
    int res = 0;
    int k = 0;
    for(int j = 0; j < 2; j++){
    vector<bool> vis(n ,false);
    int ans = 0;
    int tx = x;
    while(1){
        int cur = 0 , pos = -1,type = -1;
        if(k == 1){
            for(int i = 0; i<n; i++){
                if(tx >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            tx += cur;
        }else{
            for(int i = 0; i<n; i++){
                if(tx >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            tx += cur;
        }
    }
    k = 1;
    res = max(ans,res);
    }
    cout<<res;
}


#include<bits/stdc++.h>
#include<string>
#include<cmath>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout<< #x << '=' << x <<endl
#define MOD 1000000007

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,x;
    cin>>n>>x;
    int t[n],h[n],m[n];
    for(int i = 0; i<n; i++){
        cin>>t[i]>>h[i]>>m[i];
    }
    int ans = 0;
    vector<bool> vis(n);
    int k = -1;
    while(1){
        int cur = 0 , pos = -1,type = -1;
        if(k == -1){
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }else if(k){
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }else{
            for(int i = 0; i<n; i++){
                if(x >= h[i] && !vis[i] && k != t[i]){
                    if(m[i] > cur){
                        cur = m[i];
                        pos = i;
                        type = t[i];
                    }
                }
            }
            if(pos == -1){
                break;
            }
            vis[pos] = true;
            ans++;
            k = type;
            x += cur;
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
#define LiangJiaJun main
#define M 15000000
using namespace std;
int n,x;
struct Ds{
    int h,m;
}c[2][2004];
int cp[2];
bool vis[2][2004];
int w33ha(){
    cp[0]=cp[1]=0;
    for(int i=1;i<=n;i++){
        int t,h,m;
        scanf("%d%d%d",&t,&h,&m);
        c[t][++cp[t]].h=h;
        c[t][cp[t]].m=m;
    }
    memset(vis,0,sizeof(vis));
    int ans=0,cnt=0,sum=0;
    for(int i=1;i<=n;i++){
        int gp=-1;
        for(int j=1;j<=cp[0];j++){
            if(vis[0][j])continue;
            if(c[0][j].h>x+sum)continue;
            if(gp==-1||((!vis[0][j])&&c[0][j].m>c[0][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[0][gp]=1;
            sum+=c[0][gp].m;
        }
        gp=-1;
        for(int j=1;j<=cp[1];j++){
            if(vis[1][j])continue;
            if(c[1][j].h>x+sum)continue;
            if(gp==-1||((!vis[1][j])&&c[1][j].m>c[1][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[1][gp]=1;
            sum+=c[1][gp].m;
        }
    }
    ans=max(ans,cnt);
    memset(vis,0,sizeof(vis));
    cnt=0;sum=0;
    for(int i=1;i<=n;i++){
        int gp=-1;
        for(int j=1;j<=cp[1];j++){
            if(vis[1][j])continue;
            if(c[1][j].h>x+sum)continue;
            if(gp==-1||((!vis[1][j])&&c[1][j].m>c[1][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[1][gp]=1;
            sum+=c[1][gp].m;
        }
        gp=-1;
        for(int j=1;j<=cp[0];j++){
            if(vis[0][j])continue;
            if(c[0][j].h>x+sum)continue;
            if(gp==-1||((!vis[0][j])&&c[0][j].m>c[0][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[0][gp]=1;
            sum+=c[0][gp].m;
        }
    }
    ans=max(ans,cnt);
    printf("%d\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d",&n,&x)!=EOF)w33ha();
    return 0;
}


#include<bits/stdc++.h>
#define LiangJiaJun main
#define M 15000000
using namespace std;
int n,x;
struct Ds{
    int h,m;
}c[2][2004];
int cp[2];
bool vis[2][2004];
int w33ha(){
    cp[0]=cp[1]=0;
    for(int i=1;i<=n;i++){
        int t,h,m;
        scanf("%d%d%d",&t,&h,&m);
        c[t][++cp[t]].h=h;
        c[t][cp[t]].m=m;
    }
    memset(vis,0,sizeof(vis));
    int ans=0,cnt=0,sum=0;
    for(int i=1;i<=n;i++){
        int gp=-1;
        for(int j=1;j<=cp[0];j++){
            if(vis[0][j])continue;
            if(c[0][j].h>x+sum)continue;
            if(gp==-1||((!vis[j])&&c[0][j].m>c[0][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[0][gp]=1;
            sum+=c[0][gp].m;
        }
        gp=-1;
        for(int j=1;j<=cp[1];j++){
            if(vis[1][j])continue;
            if(c[1][j].h>x+sum)continue;
            if(gp==-1||((!vis[j])&&c[1][j].m>c[1][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[1][gp]=1;
            sum+=c[1][gp].m;
        }
    }
    ans=max(ans,cnt);
    memset(vis,0,sizeof(vis));
    cnt=0;sum=0;
    for(int i=1;i<=n;i++){
        int gp=-1;
        for(int j=1;j<=cp[1];j++){
            if(vis[1][j])continue;
            if(c[1][j].h>x+sum)continue;
            if(gp==-1||((!vis[1][j])&&c[1][j].m>c[1][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[1][gp]=1;
            sum+=c[1][gp].m;
        }
        gp=-1;
        for(int j=1;j<=cp[0];j++){
            if(vis[0][j])continue;
            if(c[0][j].h>x+sum)continue;
            if(gp==-1||((!vis[0][j])&&c[0][j].m>c[0][gp].m))gp=j;
        }
        if(gp==-1)break;
        else{
            ++cnt;
            vis[0][gp]=1;
            sum+=c[0][gp].m;
        }
    }
    ans=max(ans,cnt);
    printf("%d\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d",&n,&x)!=EOF)w33ha();
    return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int n, x;

int solve(vi &H0, vi &M0, vi &H1, vi &M1) {
    int turn = 0, ans = 0, cur = x;
    vi mark0(H0.size()), mark1(H1.size());
    while (true) {
        if (turn) {
            int ind = -1;
            for (int i = 0; i < H1.size(); ++i) {
                if (mark1[i] || H1[i] > cur) continue;
                if (ind == -1 || M1[i] > M1[ind]) ind = i;
            }
            if (ind == -1) break;
            cur += M1[ind];
            mark1[ind] = 1;
        } else {
            int ind = -1;
            for (int i = 0; i < H0.size(); ++i) {
                if (mark0[i] || H0[i] > cur) continue;
                if (ind == -1 || M0[i] > M0[ind]) ind = i;
            }
            if (ind == -1) break;
            cur += M0[ind];
            mark0[ind] = 1;
        }
        turn = !turn;
        ans++;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    vi H0, M0, H1, M1;
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        if (t) {
            H1.push_back(h);
            M1.push_back(m);
        } else {
            H0.push_back(h);
            M0.push_back(m);
        }
    }
    cout << max(solve(H0, M0, H1, M1), solve(H1, M1, H0, M0)) << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int n, x;

int solve(vii &A0, vii &A1) {
    int turn = 0, ans = 0, ind = 0, cur = x;
    while (true) {
        if (turn) {
            if (ind >= A1.size()) break;
            if (cur < A1[ind].F) break;
            cur += A1[ind].S;
            ind++;
        } else {
            if (ind >= A0.size()) break;
            if (cur < A0[ind].F) break;
            cur += A0[ind].S;
        }
        turn = !turn;
        ans++;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    vii A0, A1;
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        if (t) {
            A1.push_back({h, m});
        } else {
            A0.push_back({h, m});
        }
    }
    sort(A0.begin(), A0.end());
    sort(A1.begin(), A1.end());
    cout << max(solve(A0, A1), solve(A1, A0)) << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int,int>pii;
const int N = 2000;
vector<int>v1[N+100],v2[N+100],v3[N+100],v4[N+100];
int main()
{
	int n,x;
	scanf("%d%d",&n,&x);
	for(int i=1;i<=n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t==1){
			v1[h].push_back(m);
			v3[h].push_back(m);
		}
		else{
			v2[h].push_back(m);
			v4[h].push_back(m);
		}
	}
	for(int i=1;i<=2000;i++){
		sort(v1[i].begin(),v1[i].end());
		sort(v2[i].begin(),v2[i].end());
		sort(v3[i].begin(),v3[i].end());
		sort(v4[i].begin(),v4[i].end());
	}
	int now1 = x,now2 = x;
	int cnt1 = 0,cnt2 = 0;
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt1%2==0){
			for(int i=1;i<=now1;i++){
				if(!v1[i].empty()){
					A = max(A,mp(v1[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v1[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now1;i++){
				if(!v2[i].empty()){
					A = max(A,mp(v2[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v2[A.second].pop_back();
			}
		}
		if(!flag)break;
	}
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt1%2==0){
			for(int i=1;i<=now2;i++){
				if(!v4[i].empty()){
					A = max(A,mp(v4[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v4[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now2;i++){
				if(!v3[i].empty()){
					A = max(A,mp(v3[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v3[A.second].pop_back();
			}
		}
		if(!flag)break;
	}
	printf("%d\n",max(cnt1,cnt2));
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int,int>pii;
const int N = 2000;
vector<int>v1[N+100],v2[N+100],v3[N+100],v4[N+100];
int main()
{
	int n,x;
	scanf("%d%d",&n,&x);
	for(int i=1;i<=n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t==1){
			v1[h].push_back(m);
			v3[h].push_back(m);
		}
		else{
			v2[h].push_back(m);
			v4[h].push_back(m);
		}
	}
	for(int i=1;i<=2000;i++){
		sort(v1[i].begin(),v1[i].end());
		sort(v2[i].begin(),v2[i].end());
		sort(v3[i].begin(),v3[i].end());
		sort(v4[i].begin(),v4[i].end());
	}
	int now1 = x,now2 = x;
	int cnt1 = 0,cnt2 = 0;
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt1%2==0){
			for(int i=1;i<=now1;i++){
				if(!v1[i].empty()){
					A = max(A,mp(v1[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v1[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now1;i++){
				if(!v2[i].empty()){
					A = max(A,mp(v2[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v2[A.second].pop_back();
			}
		}
		now1 = min(now1,2000);
		if(!flag)break;
	}
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt2%2==0){
			for(int i=1;i<=now2;i++){
				if(!v4[i].empty()){
					A = max(A,mp(v4[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v4[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now2;i++){
				if(!v3[i].empty()){
					A = max(A,mp(v3[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v3[A.second].pop_back();
			}
		}
		now2 = min(now2,2000);
		if(!flag)break;
	}
	printf("%d\n",max(cnt1,cnt2));
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int,int>pii;
const int N = 2000;
vector<int>v1[N+100],v2[N+100],v3[N+100],v4[N+100];
int main()
{
	int n,x;
	scanf("%d%d",&n,&x);
	for(int i=1;i<=n;i++){
		int t,h,m;
		scanf("%d%d%d",&t,&h,&m);
		if(t==1){
			v1[h].push_back(m);
			v3[h].push_back(m);
		}
		else{
			v2[h].push_back(m);
			v4[h].push_back(m);
		}
	}
	for(int i=1;i<=2000;i++){
		sort(v1[i].begin(),v1[i].end());
		sort(v2[i].begin(),v2[i].end());
		sort(v3[i].begin(),v3[i].end());
		sort(v4[i].begin(),v4[i].end());
	}
	int now1 = x,now2 = x;
	int cnt1 = 0,cnt2 = 0;
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt1%2==0){
			for(int i=1;i<=now1;i++){
				if(!v1[i].empty()){
					A = max(A,mp(v1[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v1[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now1;i++){
				if(!v2[i].empty()){
					A = max(A,mp(v2[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt1++;
				now1 += A.first;
				v2[A.second].pop_back();
			}
		}
		now1 = min(now1,2000);
		if(!flag)break;
	}
	while(1)
	{
		pii A = mp(0,0);
		bool flag = true;
		if(cnt1%2==0){
			for(int i=1;i<=now2;i++){
				if(!v4[i].empty()){
					A = max(A,mp(v4[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v4[A.second].pop_back();
			}
		}
		else{
			for(int i=1;i<=now2;i++){
				if(!v3[i].empty()){
					A = max(A,mp(v3[i].back(),i));
				}
			}
			if(A==mp(0,0))flag = false;
			else{
				cnt2++;
				now2 += A.first;
				v3[A.second].pop_back();
			}
		}
		now2 = min(now2,2000);
		if(!flag)break;
	}
	printf("%d\n",max(cnt1,cnt2));
}


/*
মনেরে আজ কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
কেউ বা তোমায় ভালোবাসে
কেউ বা বাসতে পারে না যে,
কেউ বিকিয়ে আছে, কেউ বা
সিকি পয়সা ধারে না যে,
কতকটা যে স্বভাব তাদের
কতকটা বা তোমারো ভাই,
কতকটা এ ভবের গতিক –
সবার তরে নহে সবাই।
তোমায় কতক ফাঁকি দেবে
তুমিও কতক দেবে ফাঁকি,
তোমার ভোগে কতক পড়বে
পরের ভোগে থাকবে বাকি,
মান্ধাতারই আমল থেকে
চলে আসছে এমনি রকম –
তোমারি কি এমন ভাগ্য
বাঁচিয়ে যাবে সকল জখম!
মনেরে আজ কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
অনেক ঝঞ্ঝা কাটিয়ে বুঝি
এলে সুখের বন্দরেতে,
জলের তলে পাহাড় ছিল
লাগল বুকের অন্দরেতে,
মুহূর্তেকে পাঁজরগুলো
উঠল কেঁপে আর্তরবে –
তাই নিয়ে কি সবার সঙ্গে
ঝগড়া করে মরতে হবে?
ভেসে থাকতে পার যদি
সেইটে সবার চেয়ে শ্রেয়,
না পার তো বিনা বাক্যে
টুপ করিয়া ডুবে যেয়ো।
এটা কিছু অপূর্ব নয়,
ঘটনা সামান্য খুবই –
শঙ্কা যেথায় করে না কেউ
সেইখানে হয় জাহাজ-ডুবি।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
তোমার মাপে হয় নি সবাই
তুমিও হও নি সবার মাপে,
তুমি মর কারো ঠেলায়
কেউ বা মরে তোমার চাপে –
তবু ভেবে দেখতে গেলে
এমনি কিসের টানাটানি?
তেমন করে হাত বাড়ালে
সুখ পাওয়া যায় অনেকখানি।
আকাশ তবু সুনীল থাকে,
মধুর ঠেকে ভোরের আলো,
মরণ এলে হঠাৎ দেখি
মরার চেয়ে বাঁচাই ভালো।
যাহার লাগি চক্ষু বুজে
বহিয়ে দিলাম অশ্রুসাগর
তাহারে বাদ দিয়েও দেখি
বিশ্বভুবন মস্ত ডাগর।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
নিজের ছায়া মস্ত করে
অস্তাচলে বসে বসে
আঁধার করে তোল যদি
জীবনখানা নিজের দোষে,
বিধির সঙ্গে বিবাদ করে
নিজের পায়েই কুড়ুল মার,
দোহাই তবে এ কার্যটা
যত শীঘ্র পার সারো।
খুব খানিকটে কেঁদে কেটে
অশ্রু ঢেলে ঘড়া ঘড়া
মনের সঙ্গে এক রকমে
করে নে ভাই, বোঝাপড়া।
তাহার পরে আঁধার ঘরে
প্রদীপখানি জ্বালিয়ে তোলো –
ভুলে যা ভাই, কাহার সঙ্গে
কতটুকুন তফাত হল।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে

বোঝাপড়া -রবীন্দ্রনাথ ঠাকুর
*/

/*
          ঘরে থাকুন , সুস্থ থাকুন ।
    stay home , stay safe .
    wan-_-s
*/
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define stay_home_stay_safe return 0
#define pb push_back
#define __ ios_base::sync_with_stdio(false); cin.tie(NULL);

/// void scan(ll &n) { scanf("%lld",&n) ; }
/// void scan(ll &n, ll &m) { scanf("%lld%lld",&n,&m) ; }
/// void scan(vector<ll> &v) { ll n = v.size() ; for(ll i=0 ;i<n;i++) scanf("%lld",&v[i]) ; }

 const ll N = 100007 ;
/// vector<ll> adj[N] ;
/// void edge (ll u , ll v) {    adj[u].pb(v) ;    adj[v].pb(u) ; }

void solve() {
    ll n , x ;
    cin >> n >> x ;
    ll i ,xx=x ;
    ll t[n+1] , h[n+1],use[n+1] , m[n+1] ;
    for (i = 0 ; i< n ; i++) {
        cin >> t[i] >> h[i] >> m[i] ;
        use[i] = 0 ;
    }
    ll ans = 0 ,res = -1 ,now=0 ,idx ,cnt1=0 , cnt2=0 ;
    while(1) {
        res = -1 ;
        for (i = 0;i< n ; i++) {
            if (t[i]!=now and use[i]==0) {
                if (m[i]>res and h[i]<=x) {
                    res = m[i] ;
                    idx = i ;
                }
            }
        }

        if (res==-1) break ;
        x+=res ;
        now = t[idx] ;
        cnt1++ ;
        use[idx] = 1 ;
    }
    memset(use,0,sizeof use) ;
    now = 1;
    x=xx ;
    while(1) {
        res = -1 ;
        for (i = 0;i< n ; i++) {
            if (t[i]!=now and use[i]==0) {
                if (m[i]>res and h[i]<=x) {
                    res = m[i] ;
                    idx = i ;
                }
            }
        }

        if (res==-1) break ;
        x+=res ;
        now = t[idx] ;
        cnt2++ ;
        use[idx] = 1 ;
    }

    cout << max(cnt1,cnt2) << endl ;

}

int main() {
    __ ;
    ll t = 1 ;
   // cin >> t ;
    while(t--) { solve() ;}
    stay_home_stay_safe ;
}


/*
মনেরে আজ কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
কেউ বা তোমায় ভালোবাসে
কেউ বা বাসতে পারে না যে,
কেউ বিকিয়ে আছে, কেউ বা
সিকি পয়সা ধারে না যে,
কতকটা যে স্বভাব তাদের
কতকটা বা তোমারো ভাই,
কতকটা এ ভবের গতিক –
সবার তরে নহে সবাই।
তোমায় কতক ফাঁকি দেবে
তুমিও কতক দেবে ফাঁকি,
তোমার ভোগে কতক পড়বে
পরের ভোগে থাকবে বাকি,
মান্ধাতারই আমল থেকে
চলে আসছে এমনি রকম –
তোমারি কি এমন ভাগ্য
বাঁচিয়ে যাবে সকল জখম!
মনেরে আজ কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
অনেক ঝঞ্ঝা কাটিয়ে বুঝি
এলে সুখের বন্দরেতে,
জলের তলে পাহাড় ছিল
লাগল বুকের অন্দরেতে,
মুহূর্তেকে পাঁজরগুলো
উঠল কেঁপে আর্তরবে –
তাই নিয়ে কি সবার সঙ্গে
ঝগড়া করে মরতে হবে?
ভেসে থাকতে পার যদি
সেইটে সবার চেয়ে শ্রেয়,
না পার তো বিনা বাক্যে
টুপ করিয়া ডুবে যেয়ো।
এটা কিছু অপূর্ব নয়,
ঘটনা সামান্য খুবই –
শঙ্কা যেথায় করে না কেউ
সেইখানে হয় জাহাজ-ডুবি।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
তোমার মাপে হয় নি সবাই
তুমিও হও নি সবার মাপে,
তুমি মর কারো ঠেলায়
কেউ বা মরে তোমার চাপে –
তবু ভেবে দেখতে গেলে
এমনি কিসের টানাটানি?
তেমন করে হাত বাড়ালে
সুখ পাওয়া যায় অনেকখানি।
আকাশ তবু সুনীল থাকে,
মধুর ঠেকে ভোরের আলো,
মরণ এলে হঠাৎ দেখি
মরার চেয়ে বাঁচাই ভালো।
যাহার লাগি চক্ষু বুজে
বহিয়ে দিলাম অশ্রুসাগর
তাহারে বাদ দিয়েও দেখি
বিশ্বভুবন মস্ত ডাগর।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে।
নিজের ছায়া মস্ত করে
অস্তাচলে বসে বসে
আঁধার করে তোল যদি
জীবনখানা নিজের দোষে,
বিধির সঙ্গে বিবাদ করে
নিজের পায়েই কুড়ুল মার,
দোহাই তবে এ কার্যটা
যত শীঘ্র পার সারো।
খুব খানিকটে কেঁদে কেটে
অশ্রু ঢেলে ঘড়া ঘড়া
মনের সঙ্গে এক রকমে
করে নে ভাই, বোঝাপড়া।
তাহার পরে আঁধার ঘরে
প্রদীপখানি জ্বালিয়ে তোলো –
ভুলে যা ভাই, কাহার সঙ্গে
কতটুকুন তফাত হল।
মনেরে তাই কহ যে,
ভালো মন্দ যাহাই আসুক
সত্যেরে লও সহজে

বোঝাপড়া -রবীন্দ্রনাথ ঠাকুর
*/

/*
          ঘরে থাকুন , সুস্থ থাকুন ।
    stay home , stay safe .
    wan-_-s
*/
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define stay_home_stay_safe return 0
#define pb push_back
#define __ ios_base::sync_with_stdio(false); cin.tie(NULL);

/// void scan(ll &n) { scanf("%lld",&n) ; }
/// void scan(ll &n, ll &m) { scanf("%lld%lld",&n,&m) ; }
/// void scan(vector<ll> &v) { ll n = v.size() ; for(ll i=0 ;i<n;i++) scanf("%lld",&v[i]) ; }

 const ll N = 100007 ;
/// vector<ll> adj[N] ;
/// void edge (ll u , ll v) {    adj[u].pb(v) ;    adj[v].pb(u) ; }

void solve() {
    ll n , x ;
    cin >> n >> x ;
    ll i ;
    ll t[n+1] , h[n+1],use[n+1] , m[n+1] ;
    for (i = 0 ; i< n ; i++) {
        cin >> t[i] >> h[i] >> m[i] ;
        use[i] = 0 ;
    }
    ll ans = 0 ,res = -1 ,now=-1 ,idx ;
    while(1) {
        res = -1 ;
        for (i = 0;i< n ; i++) {
            if (t[i]!=now and use[i]==0) {
                if (m[i]>res and h[i]<=x) {
                    res = m[i] ;
                    idx = i ;
                }
            }
        }

        if (res==-1) break ;
        x+=res ;
        now = t[idx] ;
        ans++ ;
        use[idx] = 1 ;
    }
    cout << ans << endl ;

}

int main() {
    __ ;
    ll t = 1 ;
   // cin >> t ;
    while(t--) { solve() ;}
    stay_home_stay_safe ;
}


#include<bits/stdc++.h>
using namespace std;
#define cs const
#define int long long
#define low(i) (i&(-i))
#define mid ((l+r)>>1)
#define ls (now<<1)
#define rs (now<<1|1)
#define par pair<int,int>
#define mp make_pair
#define fr first
#define se second
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define drep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])
cs int base = 233;
cs int ff = 3e6+1;
cs int mod = 1145141;
cs int inf = 1e17+1;
int n, m, a[ff], top, k;
struct node { int id, cnt; } q[ff];
void init()
{
	cin >> n >> k;
	rep(i, 1, n)
		scanf("%lld", &a[i]);
	sort(a+1, a+1+n);
	rep(i, 1, n)
		if( a[i] != a[i-1] ) q[++top].id = a[i], q[top].cnt = 1;
		else
		{
			q[top].cnt++;
			if( q[top].cnt >= k ) cout << "0", exit(0);
		}
	int su = 0, del = 0;
	drep(i, top, 1)
		del += ( q[i+1].id - q[i].id ) * su, su += q[i].cnt;
	int s = 0, de = 0, ans = inf;
//	rep(i, 1, top)
//		cout << q[i].id<< " " <<q[i].cnt << endl;
//	cout << su << " " << del << endl;
	rep(i, 1, top)
	{
		// s from first  su from last
		// modify --> q[i].id
		int co = k - q[i].cnt;
		if( co <= 0 ) puts("0"), exit(0);
		su -= q[i].cnt;
		if( s < co && su < co ) ans = min(ans, del + de - (n-k) ); //only need k  abs() = 1
		else if( s < co ) ans = min(ans, del - (su-co) );
		else if( su < co ) ans = min(ans, de - (s-co) );
		else ans = min( ans, min(del - (su-co), de - (s-co) ) );
		del -= ( q[i+1].id - q[i].id ) * su; s += q[i].cnt;
		de += ( q[i+1].id - q[i].id ) * s;
	}
	cout << ans;
}
signed main()
{
	int T = 1;
	while(T--)
		init();
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+7;
ll pre[maxn],suf[maxn];int a[maxn];
map<int,int>mp;
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';ch=getchar();
	}return x*f;
}
int main() {
	int n,k;n=read(),k=read();
	for(int i=1;i<=n;i++) a[i]=read();sort(a+1,a+1+n);
	for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;i--) suf[i]=suf[i+1]+a[i];
	int opt=-1;
	for(int i=1;i<=n;i++) {
		mp[a[i]]++;opt=max(opt,mp[a[i]]);
	}if(opt>=k) {
		cout<<0<<endl;return 0;
	}ll ans=0x3f3f3f3f3f3f3f3f;
	for(ll i=1;i<=n;i++) {
		if(i<k) ans=min(ans,i*a[i]-pre[i]+suf[i]-(n-i+1)*a[i]-(n-k));
		else ans=min(ans,i*a[i]-pre[i]);
		if(i>n-k+1) ans=min(ans,suf[i]-(n-i+1)*a[i]+i*a[i]-pre[i]-(n-k));
		else ans=min(ans,suf[i]-(n-i+1)*a[i]-(n-k+1-i));
	}cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
long long a[maxn];
long long s[maxn],ans,n,k;
bool cmp(int x,int y){
	return x<y;
}
int main(){
//	freopen("makek.in","r",stdin);
//	freopen("makek.out","w",stdout);
	long long i,j,d;
	long long ans1,ans2;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1,cmp);
	for(i=1;i<=n;i++)
		s[i]=s[i-1]+a[i];
	ans=s[n];
	for(i=1;i<=n;i=j+1){
		for(j=i;j<=n;j++)
			if(a[j]>a[i])
				break;
		j--;
		if(j-i+1>=k){
			printf("0\n");
			return 0;
		}
		d=k-(j-i+1);
		if(i-1>=d&&n-j>=d){
			ans1=a[i]*(i-1)-s[i-1]-(i-1);
			ans2=s[n]-s[j]-(n-j)*a[j]-(n-j);
			ans=min(ans,min(ans1,ans2)+d);
		}
		if(i-1>=d&&n-j<d){
			ans1=a[i]*(i-1)-s[i-1]-(i-1);
			ans=min(ans,ans1+d);
		}
		if(i-1<d&&n-j>=d){
			ans2=s[n]-s[j]-(n-j)*a[j]-(n-j);
			ans=min(ans,ans2+d);
		}
		if(i-1<d&&n-j<d){
			ans1=a[i]*(i-1)-s[i-1]-(i-1);
			ans2=s[n]-s[j]-(n-j)*a[j]-(n-j);
			ans=min(ans,ans1+ans2+d);
		}
	}
	printf("%lld\n",ans);
	return 0;
}