#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX;
const int mod = 1000000007;
const int maxn = 1000001;
bool vis[2000] = {};
int x, ans = 0;
struct candy {
  int t, h, m, idx;
};
bool compare(candy c1, candy c2) { return c1.m > c2.m; }
int fn(vector<candy> v) {
  for (long long i = 0; i < v.size(); i++) {
    if (!vis[v[i].idx] && v[i].h <= x) {
      ans++;
      x += v[i].m;
      vis[v[i].idx] = 1;
      return 1;
    }
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long f = 0, q = 1, j, y, i, n, m, k;
  while (q--) {
    cin >> n >> x;
    int X = x;
    vector<candy> v, a, b;
    int t, h, m;
    for (auto i = 0; i < n; i++) {
      cin >> t >> h >> m;
      v.push_back({t, h, m, i});
    }
    sort(v.begin(), v.end(), compare);
    for (auto i = 0; i < n; i++) {
      if (v[i].t == 0)
        a.push_back(v[i]);
      else
        b.push_back(v[i]);
    }
    int max0;
    while (1) {
      if (fn(a) == 0) break;
      if (fn(b) == 0) break;
    }
    max0 = ans;
    ans = 0;
    memset(vis, 0, sizeof(vis));
    x = X;
    while (1) {
      if (fn(b) == 0) break;
      if (fn(a) == 0) break;
    }
    cout << max(ans, max0);
    cout << "\n";
  }
}
