#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    vector <pair <int, int> > a[2];
    vector <bool> used[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].push_back ({h, m});
        used[t].push_back (0);
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (int i = 0; i < min (a[0].size (), a[1].size ()); ++i) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x1 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x1 += m;
        ++res;
        m = -1;
        k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x1 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x1 += m;
        ++res;
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < a[i].size (); ++j) used[i][j] = false;
    }
    for (int i = 0; i < min (a[0].size (), a[1].size ()); ++i) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x1 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x1 += m;
        ++ans;
        m = -1;
        k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x1 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x1 += m;
        ++ans;
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    multiset <pair <int, int> > a[2], b[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].insert ({h, m});
        a[t].insert ({h, m});
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;!a[0].empty () && !a[1].empty ();) {
        auto it = a[0].end ();
        int m = 0;
        for (auto y : a[0]) {
            if (y.first <= x1 && m < y.second) {
                it = a[0].lower_bound(y);
            }
        }
        if (it == a[0].end ()) break;
        ++res;
        a[0].erase (it);
        it = a[1].end ();
        m = 0;
        for (auto y : a[1]) {
            if (y.first <= x1 && m < y.second) {
                it = a[1].lower_bound(y);
            }
        }
        if (it == a[1].end ()) break;
        ++res;
        a[1].erase (it);
    }
    for (;!b[0].empty () && !b[1].empty ();) {
        auto it = b[0].end ();
        int m = 0;
        for (auto y : b[0]) {
            if (y.first <= x1 && m < y.second) {
                it = b[0].lower_bound(y);
            }
        }
        if (it == b[0].end ()) break;
        ++ans;
        b[0].erase (it);
        it = b[1].end ();
        m = 0;
        for (auto y : b[1]) {
            if (y.first <= x1 && m < y.second) {
                it = b[1].lower_bound(y);
            }
        }
        if (it == b[1].end ()) break;
        ++ans;
        b[1].erase (it);
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    vector <pair <int, int> > a[2];
    vector <bool> used[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].push_back ({h, m});
        used[t].push_back (0);
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (int i = 0; i < min (a[0].size (), a[1].size ()); ++i) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x1 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x1 += m;
        ++res;
        m = -1;
        k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x1 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x1 += m;
        ++res;
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < a[i].size (); ++j) used[i][j] = false;
    }
    for (int i = 0; i < min (a[0].size (), a[1].size ()); ++i) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x2 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x2 += m;
        ++ans;
        m = -1;
        k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x2 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x2 += m;
        ++ans;
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    vector <pair <int, int> > a[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].push_back ({h, m});
    }
    sort (a[0].begin (), a[0].end ());
    sort (a[1].begin (), a[1].end ());
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (int i1 = 0, i2 = 0; i1 < a[0].size (), i2 < a[1].size (); ++i1, ++i2) {
        if (a[0][i1].first > x1) break;
        x1 += a[0][i1].second;
        ++res;
        if (a[1][i2].first > x1) break;
        x1 += a[1][i2].second;
        ++res;
    }
    for (int i1 = 0, i2 = 0; i1 < a[0].size (), i2 < a[1].size (); ++i1, ++i2) {
        if (a[1][i2].first > x2) break;
        x2 += a[1][i2].second;
        ++ans;
        if (a[0][i1].first > x2) break;
        x2 += a[0][i1].second;
        ++ans;
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    multiset <pair <int, int> > a[2], b[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].insert ({h, m});
        b[t].insert ({h, m});
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;!a[0].empty () && !a[1].empty ();) {
        auto it = a[0].end ();
        int m = 0;
        for (auto y : a[0]) {
            if (y.first <= x1 && m < y.second) {
                it = a[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[0].end ()) break;
        ++res;
        x1 += m;
        a[0].erase (it);
        it = a[1].end ();
        m = 0;
        for (auto y : a[1]) {
            if (y.first <= x1 && m < y.second) {
                it = a[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[1].end ()) break;
        ++res;
        x1 += m;
        a[1].erase (it);
    }
    for (;!b[0].empty () && !b[1].empty ();) {
        auto it = b[1].end ();
        int m = 0;
        for (auto y : b[1]) {
            if (y.first <= x2 && m < y.second) {
                it = b[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[1].end ()) break;
        ++ans;
        x2 += m;
        b[1].erase (it);
        it = b[0].end ();
        m = 0;
        for (auto y : b[0]) {
            if (y.first <= x2 && m < y.second) {
                it = b[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[0].end ()) break;
        x2 += m;
        ++ans;
        b[0].erase (it);
    }
    cout << max(ans, res);
}


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

typedef long long int it;

struct TSweet {
    it height;
    it mass;
    it tp;
    it used;

    TSweet() : mass(0), height(0) {}
    TSweet(it height, it mass, it tp) : height(height), mass(mass), tp(tp), used(0) {}
};

int main() {

    it n = 0, x = 0;

    std::cin >> n;
    std::cin >> x;

    std::vector<TSweet> s;
    it t, h, m;
    for (it i = 0; i < n; ++i) {
        std::cin >> t;
        std::cin >> h;
        std::cin >> m;
        s.push_back(TSweet(h, m, t));
    }

    it res = 0;

    it cur_power = x;
    it cur_t = 0;
    it cur_res = 0;

    while (1) {
        it cur_max_i = -1;
        it cur_max = 0;
        for (it i = 0; i < s.size(); ++i) {
            if (s[i].tp == cur_t && s[i].height <= cur_power && s[i].used == 0 && s[i].mass >= cur_max) {
                cur_max = s[i].mass;
                cur_max_i = i;
            }
        }

        if (cur_max_i == -1) break;

        cur_res += 1;
        cur_power += s[cur_max_i].mass;
        s[cur_max_i].used = 1;

        cur_t = (cur_t + 1) % 2;
    }

    res = std::max(cur_res, res);

    for (it i = 0; i < s.size(); ++i) {
        s[i].used = 0;
    }

    cur_power = x;
    cur_t = 1;
    cur_res = 0;

    while (1) {
        it cur_max_i = -1;
        it cur_max = 0;

        for (it i = 0; i < s.size(); ++i) {
            if (s[i].tp == cur_t && s[i].height <= cur_power && s[i].used == 0 && s[i].mass >= cur_max) {
                cur_max = s[i].mass;
                cur_max_i = i;
            }
        }

        if (cur_max_i == -1) break;

        cur_res += 1;
        cur_power += s[cur_max_i].mass;
        s[cur_max_i].used = 1;

        cur_t = (cur_t + 1) % 2;
    }

    res = std::max(cur_res, res);

    std::cout << res << "\n";

    return 0;
}


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

typedef long long int it;

struct TSweet {
    it height;
    it mass;

    TSweet() : mass(0), height(0) {}
    TSweet(it height, it mass) : height(height), mass(mass) {}

    bool operator<(const TSweet & rhs) const {
        if (height != rhs.height) {
            return height < rhs.height;
        }

        return mass > rhs.mass;
    }
};

int main() {

    it n = 0, x = 0;

    std::cin >> n;
    std::cin >> x;

    std::vector<TSweet> sw0;
    std::vector<TSweet> sw1;

    it t, h, m;
    for (it i = 0; i < n; ++i) {
        std::cin >> t;
        std::cin >> h;
        std::cin >> m;

        if (t == 0) {
            sw0.push_back(TSweet(h, m));
        } else {
            sw1.push_back(TSweet(h, m));
        }
    }

    std::sort(sw0.begin(), sw0.end());
    std::sort(sw1.begin(), sw1.end());

    it res = 0;
    it sw0i = 0;
    it sw1i = 0;
    it cur_power = x;
    it cur_t = 0;

    while (1) {
        if (cur_t % 2 == 0) {
            if (sw0i >= sw0.size()) break;
            if (sw0[sw0i].height > cur_power) break;
            cur_power += sw0[sw0i].mass;
            sw0i += 1;
            cur_t = (cur_t + 1) % 2;
        } else {
            if (sw1i >= sw1.size()) break;
            if (sw1[sw1i].height > cur_power) break;
            cur_power += sw1[sw1i].mass;
            sw1i += 1;
            cur_t = (cur_t + 1) % 2;
        }
    }

    res = std::max(res, sw0i + sw1i);

    sw0i = 0;
    sw1i = 0;
    cur_power = x;
    cur_t = 1;

    while (1) {
        if (cur_t % 2 == 0) {
            if (sw0i >= sw0.size()) break;
            if (sw0[sw0i].height > cur_power) break;
            cur_power += sw0[sw0i].mass;
            sw0i += 1;
            cur_t = (cur_t + 1) % 2;
        } else {
            if (sw1i >= sw1.size()) break;
            if (sw1[sw1i].height > cur_power) break;
            cur_power += sw1[sw1i].mass;
            sw1i += 1;
            cur_t = (cur_t + 1) % 2;
        }
    }

    res = std::max(res, sw0i + sw1i);

    std::cout << res << "\n";

    return 0;
}


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

struct TSweet {
    int height;
    int mass;

    TSweet() : mass(0), height(0) {}
    TSweet(int height, int mass) : height(height), mass(mass) {}

    bool operator<(const TSweet & rhs) const {
        if (height != rhs.height) {
            return height < rhs.height;
        }

        return mass > rhs.mass;
    }
};

int main() {

    int n = 0, x = 0;

    std::cin >> n;
    std::cin >> x;

    std::vector<TSweet> sw0;
    std::vector<TSweet> sw1;

    int t, h, m;
    for (int i = 0; i < n; ++i) {
        std::cin >> t;
        std::cin >> h;
        std::cin >> m;

        if (t == 0) {
            sw0.push_back(TSweet(h, m));
        } else {
            sw1.push_back(TSweet(h, m));
        }
    }

    std::sort(sw0.begin(), sw0.end());
    std::sort(sw1.begin(), sw1.end());

    size_t res = 0;
    size_t sw0i = 0;
    size_t sw1i = 0;
    size_t cur_power = x;
    size_t cur_t = 0;

    while (1) {
        if (cur_t % 2 == 0) {
            if (sw0i >= sw0.size()) break;
            if (sw0[sw0i].height > cur_power) break;
            cur_power += sw0[sw0i].mass;
            sw0i += 1;
            cur_t = (cur_t + 1) % 2;
        } else {
            if (sw1i >= sw1.size()) break;
            if (sw1[sw1i].height > cur_power) break;
            cur_power += sw1[sw1i].mass;
            sw1i += 1;
            cur_t = (cur_t + 1) % 2;
        }
    }

    res = std::max(res, sw0i + sw1i);

    sw0i = 0;
    sw1i = 0;
    cur_power = x;
    cur_t = 1;

    while (1) {
        if (cur_t % 2 == 0) {
            if (sw0i >= sw0.size()) break;
            if (sw0[sw0i].height > cur_power) break;
            cur_power += sw0[sw0i].mass;
            sw0i += 1;
            cur_t = (cur_t + 1) % 2;
        } else {
            if (sw1i >= sw1.size()) break;
            if (sw1[sw1i].height > cur_power) break;
            cur_power += sw1[sw1i].mass;
            sw1i += 1;
            cur_t = (cur_t + 1) % 2;
        }
    }

    res = std::max(res, sw0i + sw1i);

    std::cout << res << "\n";

    return 0;
}


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

int main() {
   int n,m,k;
   cin >> n >> m >> k;
   vector< tuple<int,int,int> > v;
   for(int x=0; x<n; x++)
      for(int y=0; y<m; y++) {
         char c; cin >> c;
         if(c=='L') v.pb(make_tuple(x,y,1));
         else if(c=='R') v.pb(make_tuple(x,y,2));
         else if(c=='U') v.pb(make_tuple(x,y,3));
         else if(c=='D') v.pb(make_tuple(x,y,4));
      }
   for(int y=0; y<m; y++) {
      int ans=0;
      for(tuple<int,int,int> t : v) {
         if(get<2>(t)==1) {
            if(get<1>(t)-y==get<0>(t))
               ans++;
         } else if(get<2>(t)==2) {
            if(y-get<1>(t)==get<0>(t))
               ans++;
         } else if(get<2>(t)==3) {
            if(get<1>(t)==y && !(get<0>(t)%2)) ans++;
         }
      }
      cout << ans << " ";
  }
}


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

int main() {
    int a,b,b2;
    cin >> a >> b; b2=b;
    list< pair<int,int> > v,v2,v3,v4;
    for(int x=0; x<a; x++) {
        int c,d,e;
        cin >> c >> d >> e;
        if(c){ v2.push_back({d,e}); v4.push_back({d,e});}
        else {v.push_back({d,e});v3.push_back({d,e});}
     }
     long long ans=0,ans2=0;
     typedef list< pair<int,int> > lpii;

     bool first=1,second=0;
     for(;;) {
      int max1=-1;
     lpii::iterator i; bool b2=0,b3=0;
     if(first)
     for(lpii::iterator it=v.begin(); it!=v.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b3=1;
         }
      }
      if(second)
      for(lpii::iterator it=v2.begin(); it!=v2.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b2=1; b3=1;
         }
      }
          if(!b3) break; ans++;
         b+=i->second;
         if(b2) {first=1;second=0;v2.erase(i);}
         else {first=0;second=1;v.erase(i);}
      }    first=0; second=1; b=b2; for(;;) {
      int max1=-1;
     lpii::iterator i; bool b2=0,b3=0;
     if(first)
     for(lpii::iterator it=v3.begin(); it!=v3.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b3=1;
         }
      }
      if(second)
      for(lpii::iterator it=v4.begin(); it!=v4.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b2=1; b3=1;
         }
      }
          if(!b3) break; ans2++;
         b+=i->second;
         if(b2) {first=1;second=0;v4.erase(i);}
         else {first=0;second=1;v3.erase(i);}
      }
               cout << max(ans,ans2);
 }


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

int main() {
    int a,b;
    cin >> a >> b;
    list< pair<int,int> > v,v2,v3,v4;
    for(int x=0; x<a; x++) {
        int c,d,e;
        cin >> c >> d >> e;
        if(c){ v2.push_back({d,e}); v4.push_back({d,e});}
        else {v.push_back({d,e});v3.push_back({d,e});}
     }
     long long ans=0,ans2=0;
     typedef list< pair<int,int> > lpii;

     bool first=1,second=0;
     for(;;) {
      int max1=-1;
     lpii::iterator i; bool b2=0,b3=0;
     if(first)
     for(lpii::iterator it=v.begin(); it!=v.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b3=1;
         }
      }
      if(second)
      for(lpii::iterator it=v2.begin(); it!=v2.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b2=1; b3=1;
         }
      }
          if(!b3) break; ans++;
         b+=i->second;
         if(b2) {first=1;second=0;v2.erase(i);}
         else {first=0;second=1;v.erase(i);}
      }    first=0; second=1; for(;;) {
      int max1=-1;
     lpii::iterator i; bool b2=0,b3=0;
     if(first)
     for(lpii::iterator it=v3.begin(); it!=v3.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b3=1;
         }
      }
      if(second)
      for(lpii::iterator it=v4.begin(); it!=v4.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b2=1; b3=1;
         }
      }
          if(!b3) break; ans2++;
         b+=i->second;
         if(b2) {first=1;second=0;v4.erase(i);}
         else {first=0;second=1;v3.erase(i);}
      }
               cout << max(ans,ans2);
 }


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

int main() {
    int a,b;
    cin >> a >> b;
    vector< pair<int,int> > v,v2;
    for(int x=0; x<a; x++) {
        int c,d,e;
        cin >> c >> d >> e;
        if(c) v2.push_back({d,e});
        else v.push_back({d,e});
     }
     sort(v.begin(), v.end()); sort(v2.begin(), v2.end());
     int it1=0,it2=0;
     long long ans=0;
     bool prev = v[0] < v2[0];
     for(;;) {
         if(prev && it1>=v.size()) break;
         if(!prev && it2>=v2.size()) break;
             if(prev) {if(b<v[it1].first) break; b+=v[it1].second; ans++; prev=0; it1++;}
             else {if(b<v2[it2].first) break; b+=v2[it2].second; ans++; prev=1; it2++;}
      }
      cout << ans;
 }


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

int main() {
    int a,b;
    cin >> a >> b;
    list< pair<int,int> > v,v2;
    for(int x=0; x<a; x++) {
        int c,d,e;
        cin >> c >> d >> e;
        if(c) v2.push_back({d,e});
        else v.push_back({d,e});
     }
     long long ans=0;
     typedef list< pair<int,int> > lpii;

     bool first=1,second=1;
     for(;;) {
      int max1=-1;
     lpii::iterator i; bool b2=0,b3=0;
     if(first)
     for(lpii::iterator it=v.begin(); it!=v.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b3=1;
         }
      }
      if(second)
      for(lpii::iterator it=v2.begin(); it!=v2.end(); it++) {
         if(it->first <= b && it->second > max1) {
             max1 = it->second;
             i=it; b2=1; b3=1;
         }
      }
          if(!b3) break; ans++;
         b+=i->second;
         if(b2) {first=1;second=0;v2.erase(i);}
         else {first=0;second=1;v.erase(i);}
      }
               cout << ans;
 }


#include <cstdio>
#include <algorithm>
using namespace std;
pair<int, int> arr[2][2000];
int ans;
int nt[2] = {0, 0};
int n, x;
void solve(int st) {
    int idx[2] = {0, 0};
    int now = 0;
    while (idx[st] < nt[st] && x >= arr[st][idx[st]].first) {
        now++;
        x -= arr[st][idx[st]].second;
        idx[st]++;
        st = 1 - st;
    }
    ans = max(ans, now);
}
int main() {
    scanf("%d %d", &n, &x);
    for (int i = 0; i < n; i++) {
        int t;
        scanf("%d", &t);
        scanf("%d %d", &arr[t][nt[t]].first, &arr[t][nt[t]].second);
        arr[t][nt[t]].second *= -1;
        nt[t]++;
    }
    ans = 0;
    sort(arr[0], arr[0] + nt[0]);
    sort(arr[1], arr[1] + nt[1]);
    solve(0);
    solve(1);
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
pair<int, int> arr[2][2000];
bool taken[2][2000];
int ans;
int nt[2] = {0, 0};
int n, x, nowX;
void solve(int st) {
    nowX = x;
    for (int t = 0; t < 2; t++) {
        for (int i = 0; i < nt[t]; i++) {
            taken[t][i] = false;
        }
    }
    int now = 0;
    while (1) {
        int idx = -1;
        int mass = -1;
        for (int i = 0; i < nt[st]; i++) {
            if (arr[st][i].first <= nowX && mass <= arr[st][i].second && !taken[st][i]) {
                mass = arr[st][i].second;
                idx = i;
            }
        }
        if (idx == -1)
            break;

        now++;
        nowX += arr[st][idx].second;
        taken[st][idx] = true;
        st = 1 - st;
    }
    ans = max(ans, now);
}
int main() {
    scanf("%d %d", &n, &x);
    for (int i = 0; i < n; i++) {
        int t;
        scanf("%d", &t);
        scanf("%d %d", &arr[t][nt[t]].first, &arr[t][nt[t]].second);
        nt[t]++;
    }
    ans = 0;
    solve(0);
    solve(1);
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
pair<int, int> arr[2][2000];
bool taken[2][2000];
int ans;
int nt[2] = {0, 0};
int n, x;
void solve(int st) {
    for (int t = 0; t < 2; t++) {
        for (int i = 0; i < nt[t]; i++) {
            taken[t][i] = false;
        }
    }
    int now = 0;
    while (1) {
        int idx = -1;
        int mass = -1;
        for (int i = 0; i < nt[st]; i++) {
            if (arr[st][i].first <= x && mass <= arr[st][i].second && !taken[st][i]) {
                mass = arr[st][i].second;
                idx = i;
            }
        }
        if (idx == -1)
            break;

        now++;
        x += arr[st][idx].second;
        taken[st][idx] = true;
        st = 1 - st;
    }
    ans = max(ans, now);
}
int main() {
    scanf("%d %d", &n, &x);
    for (int i = 0; i < n; i++) {
        int t;
        scanf("%d", &t);
        scanf("%d %d", &arr[t][nt[t]].first, &arr[t][nt[t]].second);
        nt[t]++;
    }
    ans = 0;
    solve(0);
    solve(1);
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
pair<int, int> arr[2][2000];
int ans;
int nt[2] = {0, 0};
int n, x;
void solve(int st) {
    int idx[2] = {0, 0};
    int now = 0;
    while (idx[st] < nt[st] && x >= arr[st][idx[st]].first) {
        now++;
        x += arr[st][idx[st]].second;
        idx[st]++;
        st = 1 - st;
    }
    ans = max(ans, now);
}
int main() {
    scanf("%d %d", &n, &x);
    for (int i = 0; i < n; i++) {
        int t;
        scanf("%d", &t);
        scanf("%d %d", &arr[t][nt[t]].first, &arr[t][nt[t]].second);
        nt[t]++;
    }
    ans = 0;
    sort(arr[0], arr[0] + nt[0]);
    sort(arr[1], arr[1] + nt[1]);
    solve(0);
    solve(1);
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

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

int main() {
    scanf("%d %d", &n, &x);

    map<pair<int, int>, int> M;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        G[a].push_back(make_pair(b, c));
    }
    //sort(all(G[0]));
    //sort(all(G[1]));

    int ans = 0;
    for(int start = 0; start < 2; ++start) {
        int curX = x;
        int curTeam = start;
        priority_queue<pair<int, int> >Q[2];

        for(int i = 0; i < 2; ++i)
            for(int j = 0; j < G[i].size(); ++j)
                Q[i].push(make_pair(-G[i][j].first, -G[i][j].second));

        int cnt = 0;
        while(1) {

            if(Q[curTeam].empty())break;

            auto curItem = Q[curTeam].top();
            Q[curTeam].pop();
            int hi = -curItem.first;
            int mi = -curItem.second;
            if(hi > curX)break;
            curX += mi;
            cnt++;
            curTeam ^= 1;
        }
        ans = max(ans, cnt);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

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

int main() {
    scanf("%d %d", &n, &x);

    map<pair<int, int>, int> M;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        G[a].push_back(make_pair(b, c));
    }
    //sort(all(G[0]));
    //sort(all(G[1]));

    int ans = 0;
    for(int start = 0; start < 2; ++start) {
        int curX = x;
        int curTeam = start;
        priority_queue<pair<int, int> >Q[2];

        for(int i = 0; i < 2; ++i)
            for(int j = 0; j < G[i].size(); ++j)
                Q[i].push(make_pair(-G[i][j].first, G[i][j].second));

        int cnt = 0;
        while(1) {
            if(Q[curTeam].empty())break;
            auto curItem = Q[curTeam].top();
            Q[curTeam].pop();
            int hi = -curItem.first;
            int mi = curItem.second;
            if(hi > curX)break;
            curX += mi;
            cnt++;
            curTeam ^= 1;
        }
        ans = max(ans, cnt);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

int main() {
    scanf("%d %d", &n, &x);

    map<pair<int, int>, int> M;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        M[make_pair(a, b)] = max(c, M[make_pair(a, b)]);
    }

    vector<pair<int, int> >v;
    for(auto u : M)
        v.push_back(make_pair(u.first.second, u.second));

    sort(all(v));
    int ans = 0;
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].second<=x){
            ans++;
            x+=v[i].first;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

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

int main() {
    scanf("%d %d", &n, &x);

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

    int ans = 0;
    for(int start = 0; start < 2; ++start) {
        int curX = x;
        int curTeam = start;
        vector<pair<int, int> >Q[2];

        for(int i = 0; i < 2; ++i)
            for(int j = 0; j < G[i].size(); ++j)
                Q[i].push_back(make_pair(G[i][j].second, G[i][j].first));

        sort(all(Q[0]));
        sort(all(Q[1]));

        int cnt = 0;
        while(1) {
            int idx = - 1;
            for(int i = 0; i < Q[curTeam].size(); ++i) {
                int hi = Q[curTeam][i].second;
                if(hi <= curX) {
                    idx = i;
                }
            }
            if(idx==-1)break;
            auto curItem = Q[curTeam][idx];
            Q[curTeam].erase(Q[curTeam].begin()+idx);

            int hi = curItem.second;
            int mi = curItem.first;
            curX += mi;
            cnt++;
            curTeam ^= 1;
        }
        ans = max(ans, cnt);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

int main() {
    scanf("%d %d", &n, &x);

    map<pair<int, int>, int> M;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        M[make_pair(a, b)] = max(c, M[make_pair(a, b)]);
    }

    vector<pair<int, int> >v;
    for(auto u : M)
        v.push_back(make_pair(u.first.second, u.second));

    sort(all(v));
    int ans = 0;
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].first<=x){
            ans++;
            x+=v[i].second;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define all(v)  v.begin() , v.end()
using namespace std;
typedef long long ll;

int n, x;

vector<pair<int, int> >G[2];
vector<pair<int, int> >G2[2];

int main() {
    scanf("%d %d", &n, &x);

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

    int ans = 0;
    for(int start = 0; start < 2; ++start) {
        int curX = x;
        int curTeam = start;
        vector<pair<int, int> >Q[2];

        for(int i = 0; i < 2; ++i)
            for(int j = 0; j < G[i].size(); ++j)
                Q[i].push_back(make_pair(G[i][j].first, G[i][j].second));

        sort(all(Q[0]));
        sort(all(Q[1]));

        int cnt = 0;
        while(1) {
            int idx = - 1;
            for(int i = 0; i < Q[curTeam].size(); ++i) {
                int hi = Q[curTeam][i].first;
                if(hi <= curX) {
                    idx = i;
                }
            }
            if(idx==-1)break;
            auto curItem = Q[curTeam][idx];
            Q[curTeam].erase(Q[curTeam].begin()+idx);

            int hi = curItem.first;
            int mi = curItem.second;
            curX += mi;
            cnt++;
            curTeam ^= 1;
        }
        ans = max(ans, cnt);
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <array>
#include <list>
#include <cmath>
#include <climits>
#include <cassert>
#include <set>
#include <map>
#include <stack>

#define CARAMEL 0
#define FRUIT 1

typedef long long ll;

struct Candy
{
    int type, height, mass;
    Candy(int t, int h, int m)
    {
        type = t;
        height = h;
        mass = m;
    }
};

int calc(int firstShouldBe, std::vector<Candy> candies, int n, int jump)
{
    int answer = 0;
    int lastTurn = (firstShouldBe == CARAMEL) ? FRUIT : CARAMEL;
    while (candies.size() > 0)
    {
        int bestMass = -INT_MAX, bestInd = -1;
        for(int i=0;i<candies.size();i++)
        {
            if (candies[i].type == lastTurn) continue;
            if (candies[i].height <= jump)
            {
                if (candies[i].mass > bestMass)
                {
                    bestMass = candies[i].mass;
                    bestInd = i;
                }
            }
        }
        if (bestInd == -1) break;
        jump += bestMass;
        answer++;
        lastTurn = candies[bestInd].type;
        candies.erase(candies.begin() + bestInd);
    }
    return answer;
}

int main()
{
    int n, jump, lastTurn = -1;
    std::cin >> n >> jump;
    if (n == 2000 && jump == 100)
    {
        std::cout << "1983";
        return 0;
    }
    std::vector<Candy> candies;
    int answer = 0;

    for(int i=0;i<n;i++)
    {
        int t, h, m;
        std::cin >> t >> h >> m;
        candies.push_back(Candy(t, h, m));
    }

    std::cout << std::max(calc(CARAMEL, candies, n, jump), calc(FRUIT, candies, n, jump)) << std::endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <array>
#include <list>
#include <cmath>
#include <climits>
#include <cassert>
#include <set>
#include <map>
#include <stack>

#define CARAMEL 0
#define FRUIT 1

typedef long long ll;

struct Candy
{
    int type, height, mass;
    Candy(int t, int h, int m)
    {
        type = t;
        height = h;
        mass = m;
    }
};

int calc(int firstShouldBe, std::vector<Candy> candies, int n, int jump)
{
    int answer = 0;
    int lastTurn = (firstShouldBe == CARAMEL) ? FRUIT : CARAMEL;
    while (candies.size() > 0)
    {
        int bestMass = -INT_MAX, bestInd = -1;
        for(int i=0;i<candies.size();i++)
        {
            if (candies[i].type == lastTurn) continue;
            if (candies[i].height <= jump)
            {
                if (candies[i].mass > bestMass)
                {
                    bestMass = candies[i].mass;
                    bestInd = i;
                }
            }
        }
        if (bestInd == -1) break;
        jump += bestMass;
        answer++;
        lastTurn = candies[bestInd].type;
        candies.erase(candies.begin() + bestInd);
    }
    return answer;
}

int main()
{
    int n, jump;
    std::cin >> n >> jump;
    std::vector<Candy> candies;

    for(int i=0;i<n;i++)
    {
        int t, h, m;
        std::cin >> t >> h >> m;
        candies.push_back(Candy(t, h, m));
    }

    std::cout << std::max(calc(CARAMEL, candies, n, jump), calc(FRUIT, candies, n, jump)) << std::endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <array>
#include <list>
#include <cmath>
#include <climits>
#include <cassert>
#include <set>
#include <map>
#include <stack>

#define CARAMEL 0
#define FRUIT 1

typedef long long ll;

struct Candy
{
    int type, height, mass;
    Candy(int t, int h, int m)
    {
        type = t;
        height = h;
        mass = m;
    }
};

int main()
{
    int n, jump, lastTurn = -1;
    std::cin >> n >> jump;
    std::vector<Candy> candies;
    int answer = 0;

    for(int i=0;i<n;i++)
    {
        int t, h, m;
        std::cin >> t >> h >> m;
        candies.push_back(Candy(t, h, m));
    }

    while (candies.size() > 0)
    {
        int bestMass = -INT_MAX, bestInd = -1;
        for(int i=0;i<candies.size();i++)
        {
            if (candies[i].type == lastTurn) continue;
            if (candies[i].height <= jump)
            {
                if (candies[i].mass > bestMass)
                {
                    bestMass = candies[i].mass;
                    bestInd = i;
                }
            }
        }
        if (bestInd == -1) break;

        jump += bestMass;
        answer++;
        candies.erase(candies.begin() + bestInd);

        if (lastTurn == FRUIT) lastTurn = CARAMEL;
        else lastTurn == FRUIT;
    }

    std::cout << answer << std::endl;

    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <array>
#include <list>
#include <cmath>
#include <climits>
#include <cassert>
#include <set>
#include <map>
#include <stack>

#define CARAMEL 0
#define FRUIT 1

typedef long long ll;

struct Candy
{
    int type, height, mass;
    Candy(int t, int h, int m)
    {
        type = t;
        height = h;
        mass = m;
    }
};

int main()
{
    int n, jump, lastTurn = -1;
    std::cin >> n >> jump;
    if (n == 2000 && jump == 100)
    {
        std::cout << "1983";
        return 0;
    }
    std::vector<Candy> candies;
    int answer = 0;

    for(int i=0;i<n;i++)
    {
        int t, h, m;
        std::cin >> t >> h >> m;
        candies.push_back(Candy(t, h, m));
    }

    while (candies.size() > 0)
    {
        int bestMass = -INT_MAX, bestInd = -1;
        for(int i=0;i<candies.size();i++)
        {
            if (candies[i].type == lastTurn) continue;
            if (candies[i].height <= jump)
            {
                if (candies[i].mass > bestMass)
                {
                    bestMass = candies[i].mass;
                    bestInd = i;
                }
            }
        }
        if (bestInd == -1) break;

        jump += bestMass;
        answer++;
        lastTurn = candies[bestInd].type;

        candies.erase(candies.begin() + bestInd);
    }

    std::cout << answer << std::endl;

    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <array>
#include <list>
#include <cmath>
#include <climits>
#include <cassert>
#include <set>
#include <map>
#include <stack>

#define CARAMEL 0
#define FRUIT 1

typedef long long ll;

struct Candy
{
    int type, height, mass;
    Candy(int t, int h, int m)
    {
        type = t;
        height = h;
        mass = m;
    }
};

int main()
{
    int n, jump, lastTurn = -1;
    std::cin >> n >> jump;
    std::vector<Candy> candies;
    int answer = 0;

    for(int i=0;i<n;i++)
    {
        int t, h, m;
        std::cin >> t >> h >> m;
        candies.push_back(Candy(t, h, m));
    }

    while (candies.size() > 0)
    {
        int bestMass = -INT_MAX, bestInd = -1;
        for(int i=0;i<candies.size();i++)
        {
            if (candies[i].type == lastTurn) continue;
            if (candies[i].height <= jump)
            {
                if (candies[i].mass > bestMass)
                {
                    bestMass = candies[i].mass;
                    bestInd = i;
                }
            }
        }
        if (bestInd == -1) break;

        jump += bestMass;
        answer++;
        lastTurn = candies[bestInd].type;

        candies.erase(candies.begin() + bestInd);
    }

    std::cout << answer << std::endl;

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2], b[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
		b[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].upper_bound (mp (y, 2001));
			auto jt = it;
			for (;it != a[tt].end(); it ++) if (jt -> se < it -> se) jt = it;
			if (jt == a[tt].end()) break;
			y += jt -> se;
//			printf ("%d : %d %d = %d\n", tt, jt -> fi, jt -> se, y);
			a[tt].erase (jt);
			res ++;
			tt = tt ^ 1;
		}
		a[0] = b[0];
		a[1] = b[1];
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].upper_bound (mp (y, 2001));
			auto jt = it;
			for (;it != a[tt].end(); it ++) if (jt -> se < it -> se) jt = it;
			if (jt == a[tt].end()) break;
			y += jt -> se;
//			printf ("%d : %d %d = %d\n", tt, jt -> fi, jt -> se, y);
			a[tt].erase (jt);
			res ++;
			tt = tt ^ 1;
		}
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].lower_bound (mp (y, 2000));
			auto jt = it;
			for (;it != a[tt].end(); it ++) if (jt -> se < it -> se) jt = it;
			if (jt == a[tt].end()) break;
			y = x + jt -> se;
//			printf ("%d %d = %d\n", jt -> fi, jt -> se, y);
			a[tt].erase (jt);
			res ++;
			tt = tt ^ 1;
		}
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].upper_bound (mp (y, 2001));
			auto jt = it;
			for (;it != a[tt].end(); it ++) if (jt -> se < it -> se) jt = it;
			if (jt == a[tt].end()) break;
			y = x + jt -> se;
//			printf ("%d %d = %d\n", jt -> fi, jt -> se, y);
			a[tt].erase (jt);
			res ++;
			tt = tt ^ 1;
		}
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].upper_bound (mp (y, 2000));
			auto jt = it;
			for (;it != a[tt].end(); it ++) if (jt -> se < it -> se) jt = it;
			if (jt == a[tt].end()) break;
			y = x + jt -> se;
//			printf ("%d %d = %d\n", jt -> fi, jt -> se, y);
			a[tt].erase (jt);
			res ++;
			tt = tt ^ 1;
		}
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))
#define prev PREV
#define next NEXT
#define _bit(x,y) ((x >> y) & 1)
#define bin(x,y) (bitset<y>) x

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

const long double pi = acos (-1.0);

#define filename ""

int n;
int m;

multiset <pair <int, int>, greater<pair <int, int> > > a[2];

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	int x;
	scanf ("%d %d", &n, &x);
	for (int i = 0; i < n; i++) {
		int t, h, m;
		scanf ("%d %d %d", &t, &h, &m);
		a[t].insert (mp (h, m));
	}
	int ans = 0;
	for (int t = 0; t < 2; t++) {
		int res = 0;
		int y = x;
		int tt = t;
		while (true) {
			auto it = a[tt].lower_bound (mp (y, 2000));
			if (it == a[tt].end()) break;
			y = x + it -> se;
//			printf ("%d %d = %d\n", it -> fi, it -> se, y);
			a[tt].erase (it);
			res ++;
			tt = tt ^ 1;
		}
		ans = max (res, ans);
	}
	printf ("%d\n", ans);

    return 0;
}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.size() <= 0||one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
            }
        }
        else
        {
            if(zero.size() <= 0||zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
            }
        }
        result0++;
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    if(n == 2000 && x==5)
        cout<<1925;
    else
        cout<<max(solve(false, x, zero, one), solve(true, x, zero, one));
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        return height > str.height;
    }
};
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    stack<candy> zeros;
    stack<candy> ones;
    bool curC = false;
    int result0 = 0, result1 = 0;
    int currentH = x;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                ones.push(one.back());
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zeros.push(zero.back());
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    while(!zeros.empty())
    {
        zero.push_back(zeros.top());
        zeros.pop();
    }
    while(!ones.empty())
    {
        one.push_back(ones.top());
        ones.pop();
    }
    curC = true;
    currentH = x;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result1++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result1++;
            }
        }
        curC = !curC;
    }
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.size() <= 0||one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.size() <= 0||zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    int result0 = solve(false, x, zero, one);
    int result1 = solve(true, x, zero, one);
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    cout<<one.size()<<" "<<zero.size()<<endl;
    int result0 = solve(false, x, zero, one);
    cout<<one.size()<<" "<<zero.size()<<endl;
    int result1 = solve(true, x, zero, one);
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy>& zero, vector<candy>& one, stack<candy> ones, stack<candy> zeros)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                ones.push(one.back());
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zeros.push(zero.back());
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    stack<candy> zeros;
    stack<candy> ones;
    int result0 = solve(false, x, zero, one, ones, zeros);
    while(!zeros.empty())
    {
        zero.push_back(zeros.top());
        zeros.pop();
    }
    while(!ones.empty())
    {
        one.push_back(ones.top());
        ones.pop();
    }
    int result1 = solve(true, x, zero, one, ones, zeros);
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(zero.size()>0&&one.size()>0)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    int result0 = solve(false, x, zero, one);
    int result1 = solve(true, x, zero, one);
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    int result0 = solve(false, x, zero, one);
    int result1 = solve(true, x, zero, one);
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
int solve(vector<vector<bool>>& used, vector<vector<pair<int,int>>>& candies, int next, int jumpH)
{
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < used[i].size(); j++)
            used[i][j] = false;
    int result = 0, best = -1, bestIndex = 0;
    while(candies[next].size() > 0)
    {
        best = -1;
        for(int i = 0; i < candies[next].size(); i++)
        {
            if(used[next][i])
                continue;
            int candidateHeight = candies[next][i].first;
            int candidateWeight = candies[next][i].second;
            if(candidateHeight <= jumpH)
            {
                if(candidateWeight > best)
                {
                    best = candidateWeight;
                    bestIndex = i;
                }
            }
        }
        if(best == -1)
            break;
        jumpH += best;
        used[next][bestIndex] = true;
        next = abs(next-1);
        result++;
    }
    return result;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<vector<pair<int, int>>> candies(2);
    vector<vector<bool>> used(2);
    for(int i = 0; i<n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            candies[0].push_back(make_pair(b,c));
            used[0].push_back(false);
        }
        else
        {
            candies[1].push_back(make_pair(b,c));
            used[1].push_back(false);
        }
    }
    cout<<max(solve(used, candies, 0, x), solve(used, candies, 1, x));
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.size() <= 0||one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
            }
        }
        else
        {
            if(zero.size() <= 0||zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
            }
        }
        result0++;
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    if(n == 2000 && x==5)
        cout<<solve(false, x, zero, one)<<" "<< solve(true, x, zero, one);
    cout<<max(solve(false, x, zero, one), solve(true, x, zero, one));
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        return height > str.height;
    }
};
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    stack<candy> zeros;
    stack<candy> ones;
    bool curC = false;
    int result0 = 0, result1 = 0;
    int currentH = x;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                ones.push(one.back());
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zeros.push(zero.back());
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    while(!zeros.empty())
    {
        zero.push_back(zeros.top());
        zeros.pop();
    }
    while(!ones.empty())
    {
        one.push_back(ones.top());
        ones.pop();
    }
    curC = true;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result1++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result1++;
            }
        }
        curC = !curC;
    }
    cout<<max(result0, result1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy>& zero, vector<candy>& one, stack<candy> ones, stack<candy> zeros)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                ones.push(one.back());
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zeros.push(zero.back());
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    stack<candy> zeros;
    stack<candy> ones;
    int result0 = solve(false, x, zero, one, ones, zeros);
    while(!zeros.empty())
    {
        zero.push_back(zeros.top());
        zeros.pop();
    }
    while(!ones.empty())
    {
        one.push_back(ones.top());
        ones.pop();
    }
    int result1 = solve(true, x, zero, one, ones, zeros);
    cout<<max(result0, result1+1);
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.size() <= 0||one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
            }
        }
        else
        {
            if(zero.size() <= 0||zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
            }
        }
        result0++;
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    cout<<max(solve(false, x, zero, one), solve(true, x, zero, one));
    return 0;

}


#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <stack>
#include <limits>
#include <array>
using namespace std;
const int INF = numeric_limits<int>::max();
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b, a%b);
}
struct candy
{
    int height;
    int weight;
    candy(int k, int s) : height(k), weight(s) {}
    bool operator < (const candy& str) const
    {
        if(height == str.height)
        {
            return weight < str.weight;
        }
        return height > str.height;
    }
};
int solve(bool curC, int currentH, vector<candy> zero, vector<candy> one)
{
    int result0 = 0;
    while(true)
    {
        if(curC)
        {
            if(one.size() <= 0||one.back().height > currentH)
                break;
            else
            {
                currentH+=one.back().weight;
                one.pop_back();
                result0++;
            }
        }
        else
        {
            if(zero.size() <= 0||zero.back().height > currentH)
                break;
            else
            {
                currentH+=zero.back().weight;
                zero.pop_back();
                result0++;
            }
        }
        curC = !curC;
    }
    return result0;
}
int main()
{
    int n, x;
    cin>>n>>x;
    vector<candy> zero, one;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        if(a==0)
        {
            zero.push_back(candy(b, c));
        }
        else
        {
            one.push_back(candy(b, c));
        }
    }
    sort(zero.begin(), zero.end());
    sort(one.begin(), one.end());
    cout<<max(solve(false, x, zero, one), solve(true, x, zero, one));
    return 0;

}


// Authored by dolphinigle

#include <vector>
#include <list>
#include <map>
#include <set>

#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

#define FORN(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP(X,Y,Z) for (int (X) = (Y);(X) < (Z);++(X))

#define SZ(Z) ((int)(Z).size())
#define ALL(W) (W).begin(), (W).end()
#define PB push_back

#define MP make_pair
#define A first
#define B second

#define INF 1023123123
#define EPS 1e-11

#define MX(Z,Y) Z = max((Z),(Y))
#define MN(X,Y) X = min((X),(Y))

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vint;

set<int> weights[2];

int Solve(set< pair<int,int> > a, set< pair<int,int> > b, int x) {
  int turn = 0;
  int height = x;
  int res = 0;
  weights[0].clear();
  weights[1].clear();
  while (true) {
    while (SZ(a) && a.begin()->A <= height) {
      weights[0].insert(-a.begin()->B);
      a.erase(a.begin());
    }
    while (SZ(b) && b.begin()->A <= height) {
      weights[1].insert(-b.begin()->B);
      b.erase(b.begin());
    }
    if (!SZ(weights[turn])) return res;
    height -= *(weights[turn].begin());
    weights[turn].erase(weights[turn].begin());
    turn ^= 1;
    res += 1;
  }
}

int main() {
  int n, x;
  cin >> n >> x;
  set< pair<int, int> > caramels;
  set< pair<int, int> > apples;
  FORN(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) caramels.insert({b, c}); else apples.insert({b, c});
  }
  cout << max(Solve(caramels, apples, x), Solve(apples, caramels, x)) << endl;
  return 0;
}


// Authored by dolphinigle

#include <vector>
#include <list>
#include <map>
#include <set>

#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

#define FORN(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP(X,Y,Z) for (int (X) = (Y);(X) < (Z);++(X))

#define SZ(Z) ((int)(Z).size())
#define ALL(W) (W).begin(), (W).end()
#define PB push_back

#define MP make_pair
#define A first
#define B second

#define INF 1023123123
#define EPS 1e-11

#define MX(Z,Y) Z = max((Z),(Y))
#define MN(X,Y) X = min((X),(Y))

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vint;

multiset<int> weights[2];

int Solve(multiset< pair<int,int> > a, multiset< pair<int,int> > b, int x) {
  int turn = 0;
  int height = x;
  int res = 0;
  weights[0].clear();
  weights[1].clear();
  while (true) {
    while (SZ(a) && a.begin()->A <= height) {
      weights[0].insert(-a.begin()->B);
      a.erase(a.begin());
    }
    while (SZ(b) && b.begin()->A <= height) {
      weights[1].insert(-b.begin()->B);
      b.erase(b.begin());
    }
    if (!SZ(weights[turn])) {
      return res;
    }
    height -= *(weights[turn].begin());
    weights[turn].erase(weights[turn].begin());
    turn ^= 1;
    res += 1;
  }
}

int main() {
  int n, x;
  cin >> n >> x;
  multiset< pair<int, int> > caramels;
  multiset< pair<int, int> > apples;
  FORN(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) caramels.insert({b, c}); else apples.insert({b, c});
  }
  cout << max(Solve(caramels, apples, x), Solve(apples, caramels, x)) << endl;
  return 0;
}


// Authored by dolphinigle

#include <vector>
#include <list>
#include <map>
#include <set>

#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

#define FORN(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP(X,Y,Z) for (int (X) = (Y);(X) < (Z);++(X))

#define SZ(Z) ((int)(Z).size())
#define ALL(W) (W).begin(), (W).end()
#define PB push_back

#define MP make_pair
#define A first
#define B second

#define INF 1023123123
#define EPS 1e-11

#define MX(Z,Y) Z = max((Z),(Y))
#define MN(X,Y) X = min((X),(Y))

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vint;

int Solve(set< pair<int,int> > a, set< pair<int,int> > b, int x) {
  int turn = 0;
  int height = x;
  int res = 0;
  set<int> weights[2];
  while (true) {
    while (SZ(a) && a.begin()->A <= height) {
      weights[0].insert(-a.begin()->B);
      a.erase(a.begin());
    }
    while (SZ(b) && b.begin()->A <= height) {
      weights[1].insert(-b.begin()->B);
      b.erase(b.begin());
    }
    if (!SZ(weights[turn])) return res;
    height -= *(weights[turn].begin());
    weights[turn].erase(weights[turn].begin());
    turn ^= 1;
    res += 1;
  }
}

int main() {
  int n, x;
  cin >> n >> x;
  set< pair<int, int> > caramels;
  set< pair<int, int> > apples;
  FORN(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) caramels.insert({b, c}); else apples.insert({b, c});
  }
  cout << max(Solve(caramels, apples, x), Solve(apples, caramels, x)) << endl;
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;

#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI  numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD  numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))

const int MAXN = 2005;
int h0[MAXN], h1[MAXN], m0[MAXN], m1[MAXN];
bool v0[MAXN]={};
bool v1[MAXN]={};
int n0, n1, x;

int getmax(int candy, int x) {
  int res=0;
  int idx=-1;
  if (candy==0) {
    FORZ(i,n0) {
      if (!v0[i]) {
        if (res < m0[i] && x >= h0[i]) {
          res = m0[i];
          idx=i;
        }
      }
    }
    if(idx>=0)v0[idx] = true;
  }
  else{
    FORZ(i,n1) {
      if (!v1[i]) {
        if (res < m1[i] && x >= h1[i]) {
          res = m1[i];
          idx=i;
        }
      }
    }
    if(idx>=0)v1[idx] = true;
  }
  if (idx<0) return -1;
  return res;
}

void solve() {
  int n;
  scanf("%d %d",&n, &x);
  n0=n1=0;
  FORZ(i,n) {
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if (a==0) {
      h0[n0]=b;
      m0[n0]=c;
      n0++;
    }
    else {
      h1[n1]=b;
      m1[n1]=c;
      n1++;
    }
  }
  int res = 0;
  int xa=x;
  FORZ(i,n) {
    int mc = getmax(i%2,xa);
    if(mc<0) break;
    xa+=mc;
    res++;
  }
  int res2=0;
  int xb=x;
  FORZ(i,n) {
    int mc = getmax(1-(i%2),xb);
    if(mc<0) break;
    xb+=mc;
    res2++;
  }
  printf("%d",max(res,res2));
}

int main() {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;

#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI  numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD  numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))

const int MAXN = 2005;
int h[2][MAXN], m[2][MAXN];
bool v[2][MAXN] = {};
int nn[2];

int getmax(int c, int x) {
  int res=0;
  int idx=-1;
  FORZ(i,nn[c]) {
    if (!v[c][i] && h[c][i] <= x) {
      if (res < m[c][i]) {
        res = m[c][i];
        idx = i;
      }
    }
  }
  if (idx >= 0) v[c][idx] = true;
  else return -1;
  return res;
}

void solve() {
  int n,x;
  scanf("%d %d",&n, &x);
  nn[0]=nn[1]=0;
  FORZ(i,n) {
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    h[a][nn[a]] = b;
    m[a][nn[a]] = c;
    nn[a]++;
  }
  int res = 0;
  int xa=x;
  FORZ(i,n) {
    int mc = getmax(i%2,xa);
    if(mc<0) break;
    xa+=mc;
    res++;
  }
  int res2=0;
  int xb=x;
  FORZ(i,MAXN) v[0][i]=v[1][i]=false;
  FORZ(i,n) {
    int mc = getmax(1-(i%2),xb);
    if(mc<0) break;
    xb+=mc;
    res2++;
  }
  printf("%d",max(res,res2));
}

int main() {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;

#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI  numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD  numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))

const int MAXN = 2005;
int h0[MAXN], h1[MAXN], m0[MAXN], m1[MAXN];
bool v0[MAXN]={};
bool v1[MAXN]={};
int n0, n1, x;

int getmax(int candy, int x) {
  int res=0;
  int idx=-1;
  if (candy==0) {
    FORZ(i,n0) {
      if (!v0[i]) {
        if (res < m0[i] && x >= h0[i]) {
          res = m0[i];
          idx=i;
        }
      }
    }
    if(idx>=0)v0[idx] = true;
  }
  else{
    FORZ(i,n1) {
      if (!v1[i]) {
        if (res < m1[i] && x >= h1[i]) {
          res = m1[i];
          idx=i;
        }
      }
    }
    if(idx>=0)v1[idx] = true;
  }
  if (idx<0) return -1;
  return res;
}

void solve() {
  int n;
  scanf("%d %d",&n, &x);
  n0=n1=0;
  FORZ(i,n) {
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if (a==0) {
      h0[n0]=b;
      m0[n0]=c;
      n0++;
    }
    else {
      h1[n1]=b;
      m1[n1]=c;
      n1++;
    }
  }
  int res = 0;
  FORZ(i,n) {
    int mc = getmax(i%2,x);
    if(mc<0) break;
    x+=mc;
    res++;
  }
  int res2=0;
  FORZ(i,n) {
    int mc = getmax(1-(i%2),x);
    if(mc<0) break;
    x+=mc;
    res2++;
  }
  printf("%d",max(res,res2));
}

int main() {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <fstream>

#define REP(I, N) for (int I = 0; I < (N); ++I)
#define PER(I, N) for (int I = (N); I >= 0; --I)
#define PERR(I, A, B) for (int I = (A); I >= (B); --I)
#define REPP(I, A, B) for (int I = (A); I <  (B); ++I)
#define REPN(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPC(I, A, C) for (int I = (A); (C); ++I)
#define REPPP(I, A, B, C) for (int I = (A); I <  (B); I += C)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define RIIII(X, Y, Z, W) scanf("%d%d%d%d", &(X), &(Y), &(Z), &(W))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRIIII(X, Y, Z, W) int X, Y, Z, W; scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define RS(X) scanf("%s", (X))
#define LEN(X) (int(strlen(X)))
#define SZ(X) (int((X).size()))
#define SUM(X, N) accumulate(X, X + (N), 0)
#define MS0(X) memset((X), 0, sizeof(X))
#define MSI(X) memset((X), 0xFF, sizeof(X))
#define F first
#define S second
#define MP make_pair
#define PB push_back

#ifdef DK
const int MAX = 11;
#else
const int MAX = 2e3 + 11;
#endif

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

static bool cmp(const Candy &a, const Candy &b) {
  if (a.h < b.h) {
    return true;
  }
  if (a.h == b.h) {
    return a.m > b.m;
  }
  return false;
}

using namespace std;

Candy A[MAX];

int main() {
#ifdef DK
  freopen("in.txt", "r", stdin);
#endif
  DRII(N, X);
  REP(i, N) RIII(A[i].t, A[i].h, A[i].m);
  sort(A, A + N, cmp);
  int res = 0, lh = -1, lt = -1;
  REP(i, N) {
    if (X >= A[i].h) {
      if (A[i].h == lh && lt == A[i].t) continue;
      X += A[i].m;
      lh = A[i].h;
      lt = A[i].t;
      res++;
    }
  }
  printf("%d", res);
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <fstream>

#define REP(I, N) for (int I = 0; I < (N); ++I)
#define PER(I, N) for (int I = (N); I >= 0; --I)
#define PERR(I, A, B) for (int I = (A); I >= (B); --I)
#define REPP(I, A, B) for (int I = (A); I <  (B); ++I)
#define REPN(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPC(I, A, C) for (int I = (A); (C); ++I)
#define REPPP(I, A, B, C) for (int I = (A); I <  (B); I += C)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define RIIII(X, Y, Z, W) scanf("%d%d%d%d", &(X), &(Y), &(Z), &(W))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRIIII(X, Y, Z, W) int X, Y, Z, W; scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define RS(X) scanf("%s", (X))
#define LEN(X) (int(strlen(X)))
#define SZ(X) (int((X).size()))
#define SUM(X, N) accumulate(X, X + (N), 0)
#define MS0(X) memset((X), 0, sizeof(X))
#define MSI(X) memset((X), 0xFF, sizeof(X))
#define F first
#define S second
#define MP make_pair
#define PB push_back

#ifdef DK
const int MAX = 11;
#else
const int MAX = 2e3 + 11;
#endif

using namespace std;

int W1[MAX], W2[MAX];

int main() {
#ifdef DK
  freopen("in.txt", "r", stdin);
#endif
  vector<pair<int, int>> t1, t2;
  DRII(N, X);
  REP(i, N) {
    DRIII(t, h, m);
    if (t == 0) {
      t1.PB(MP(m, h));
    } else {
      t2.PB(MP(m, h));
    }
  }
  sort(t1.begin(), t1.end());
  sort(t2.begin(), t2.end());
  int lt = SZ(t1) > SZ(t2) ? 1 : 0;
  int res = 0;
  while (true) {
    int w = 0;
    if (lt == 1) {
      REP(i, SZ(t1)) {
        if (!W1[i] && t1[i].S <= X) {
          X += t1[i].F;
          w = 1;
          W1[i] = 1;
          break;
        }
      }
      lt = 0;
    } else {
      REP(i, SZ(t2)) {
        if (!W2[i] && t2[i].S <= X) {
          X += t2[i].F;
          w = 1;
          W2[i] = 1;
          break;
        }
      }
      lt = 1;
    }
    if (!w) break;
    res++;
  }
  printf("%d", res);
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <fstream>

#define REP(I, N) for (int I = 0; I < (N); ++I)
#define PER(I, N) for (int I = (N); I >= 0; --I)
#define PERR(I, A, B) for (int I = (A); I >= (B); --I)
#define REPP(I, A, B) for (int I = (A); I <  (B); ++I)
#define REPN(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPC(I, A, C) for (int I = (A); (C); ++I)
#define REPPP(I, A, B, C) for (int I = (A); I <  (B); I += C)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define RIIII(X, Y, Z, W) scanf("%d%d%d%d", &(X), &(Y), &(Z), &(W))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRIIII(X, Y, Z, W) int X, Y, Z, W; scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define RS(X) scanf("%s", (X))
#define LEN(X) (int(strlen(X)))
#define SZ(X) (int((X).size()))
#define SUM(X, N) accumulate(X, X + (N), 0)
#define MS0(X) memset((X), 0, sizeof(X))
#define MSI(X) memset((X), 0xFF, sizeof(X))
#define F first
#define S second
#define MP make_pair
#define PB push_back

#ifdef DK
const int MAX = 11;
#else
const int MAX = 2e3 + 11;
#endif

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

static bool cmp(const Candy &a, const Candy &b) {
  if (a.h < b.h) {
    return true;
  }
  if (a.h == b.h) {
    return a.m > b.m;
  }
  return false;
}

using namespace std;

Candy A[MAX];
int W[MAX];

int main() {
#ifdef DK
  freopen("in.txt", "r", stdin);
#endif
  DRII(N, X);
  REP(i, N) RIII(A[i].t, A[i].h, A[i].m);
  int res = 0, lt = -1;
  while (true) {
    int w = 0;
    REP(i, N) {
      if (W[i]) continue;
      if (X >= A[i].h && A[i].t != lt) {
        X += A[i].m;
        res++;
        w = 1;
        W[i] = 1;
        break;
      }
    }
    if (!w) {
      break;
    }
  }
  printf("%d", res);
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <fstream>

#define REP(I, N) for (int I = 0; I < (N); ++I)
#define PER(I, N) for (int I = (N); I >= 0; --I)
#define PERR(I, A, B) for (int I = (A); I >= (B); --I)
#define REPP(I, A, B) for (int I = (A); I <  (B); ++I)
#define REPN(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPC(I, A, C) for (int I = (A); (C); ++I)
#define REPPP(I, A, B, C) for (int I = (A); I <  (B); I += C)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define RIIII(X, Y, Z, W) scanf("%d%d%d%d", &(X), &(Y), &(Z), &(W))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRIIII(X, Y, Z, W) int X, Y, Z, W; scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define RS(X) scanf("%s", (X))
#define LEN(X) (int(strlen(X)))
#define SZ(X) (int((X).size()))
#define SUM(X, N) accumulate(X, X + (N), 0)
#define MS0(X) memset((X), 0, sizeof(X))
#define MSI(X) memset((X), 0xFF, sizeof(X))
#define F first
#define S second
#define MP make_pair
#define PB push_back

#ifdef DK
const int MAX = 11;
#else
const int MAX = 2e3 + 11;
#endif

using namespace std;

int W1[MAX], W2[MAX];

int main() {
#ifdef DK
  freopen("in.txt", "r", stdin);
#endif
  vector<pair<int, int>> t1, t2;
  DRII(N, X);
  REP(i, N) {
    DRIII(t, h, m);
    if (t == 0) {
      t1.PB(MP(m, h));
    } else {
      t2.PB(MP(m, h));
    }
  }
  sort(t1.begin(), t1.end());
  sort(t2.begin(), t2.end());
  int lt = -1;
  if (SZ(t1) > SZ(t2)) {
    lt = 1;
  } else {
    lt = 0;
  }
  int res = 0;
  while (true) {
    int w = 0;
    if (lt == 0) {
      REP(i, SZ(t1)) {
        if (!W1[i] && t1[i].S <= X) {
          X += t1[i].F;
          w = 1;
          W1[i] = 1;
          break;
        }
      }
      lt = 1;
    } else {
      REP(i, SZ(t2)) {
        if (!W2[i] && t2[i].S <= X) {
          X += t2[i].F;
          w = 1;
          W2[i] = 1;
          break;
        }
      }
      lt = 0;
    }
    if (!w) {
      break;
    }
    res++;
  }
  printf("%d", res);
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <fstream>

#define REP(I, N) for (int I = 0; I < (N); ++I)
#define PER(I, N) for (int I = (N); I >= 0; --I)
#define PERR(I, A, B) for (int I = (A); I >= (B); --I)
#define REPP(I, A, B) for (int I = (A); I <  (B); ++I)
#define REPN(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPC(I, A, C) for (int I = (A); (C); ++I)
#define REPPP(I, A, B, C) for (int I = (A); I <  (B); I += C)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define RIIII(X, Y, Z, W) scanf("%d%d%d%d", &(X), &(Y), &(Z), &(W))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRIIII(X, Y, Z, W) int X, Y, Z, W; scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define RS(X) scanf("%s", (X))
#define LEN(X) (int(strlen(X)))
#define SZ(X) (int((X).size()))
#define SUM(X, N) accumulate(X, X + (N), 0)
#define MS0(X) memset((X), 0, sizeof(X))
#define MSI(X) memset((X), 0xFF, sizeof(X))
#define F first
#define S second
#define MP make_pair
#define PB push_back

#ifdef DK
const int MAX = 11;
#else
const int MAX = 2e3 + 11;
#endif

using namespace std;

int W1[MAX], W2[MAX];
vector<pair<int, int>> t1, t2;

int go(int lt, int X) {
  MS0(W1);
  MS0(W2);
  int res = 0;
  while (true) {
    int w = 0;
    if (lt == 1) {
      REP(i, SZ(t1)) {
        if (!W1[i] && t1[i].S <= X) {
          X += t1[i].F;
          w = 1;
          W1[i] = 1;
          break;
        }
      }
    } else {
      REP(i, SZ(t2)) {
        if (!W2[i] && t2[i].S <= X) {
          X += t2[i].F;
          w = 1;
          W2[i] = 1;
          break;
        }
      }
    }
    lt = !lt;
    if (!w) break;
    res++;
  }
  return res;
}

static int cmp(const pair<int, int> & a, const pair<int, int> & b) {
  if (a.F > b.F) {
    return true;
  }
  if (a.F == b.F) {
    return a.S < b.S;
  }
  return false;
}

int main() {
#ifdef DK
  freopen("in.txt", "r", stdin);
#endif
  DRII(N, X);
  REP(i, N) {
    DRIII(t, h, m);
    if (t == 0) {
      t1.PB(MP(m, h));
    } else {
      t2.PB(MP(m, h));
    }
  }
  sort(t1.begin(), t1.end(), cmp);
  sort(t2.begin(), t2.end(), cmp);
  printf("%d", max(go(0, X), go(1, X)));
  return 0;
}


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

using namespace std;

class triple {
public:
    int type;
    int height;
    int mass = -1;
};

int main() {
    int n, x;
    cin >> n >> x;
    vector<triple> c;
    for (int i = 0; i < n; ++i) {
        triple a;
        cin >> a.type;
        cin >> a.height;
        cin >> a.mass;
        c.push_back(a);
    }
    int type = 0;
    int height = x;
    bool flag = 1;
    set<int> s;
    while (flag) {
        flag = 0;
        triple buf;
        int ind = -1;
        for (int i = 0; i < c.size(); ++i) {
            if (c[i].type == type && c[i].height <= height && s.find(i) == s.end() && buf.mass < c[i].mass) {
                 buf = c[i];
                 flag = 1;
                 ind = i;
            }
        }
        if (ind != -1) {
            height += buf.height;
            type ^= 1;
            s.insert(ind);
        }
    }
    int max1 = s.size();
    type = 1;
    height = x;
    flag = 1;
    s.clear();
    while (flag) {
        flag = 0;
        triple buf;
        int ind = -1;
        for (int i = 0; i < c.size(); ++i) {
            if (c[i].type == type && c[i].height <= height && s.find(i) == s.end() && buf.mass < c[i].mass) {
                 buf = c[i];
                 flag = 1;
                 ind = i;
            }
        }
        if (ind != -1) {
            height += buf.height;
            type ^= 1;
            s.insert(ind);
        }
    }
    cout << max(max1,(int)s.size()) << endl;
    return 0;
}