#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[2005], b[2005], c[2005];
bool v[2005];
void Solve() {
  int i, j, k, t, p;
  cin >> n >> m;
  int ans = 0;
  for (i = 1; i <= n; ++i) cin >> a[i] >> b[i] >> c[i];
  memset(v, 0, sizeof(v));
  int now = m, gun = 1;
  for (i = 1; i <= n; ++i) {
    t = 0;
    for (j = 1; j <= n; ++j)
      if (!v[j] && now >= b[j] && a[j] == gun)
        if (c[j] > t) t = c[j], p = j;
    if (t == 0) break;
    now += t;
    v[p] = 1;
    gun = 1 - gun;
  }
  ans = i - 1;
  now = m, gun = 0;
  memset(v, 0, sizeof(v));
  for (i = 1; i <= n; ++i) {
    t = 0;
    for (j = 1; j <= n; ++j)
      if (!v[j] && now >= b[j] && a[j] == gun)
        if (c[j] > t) t = c[j], p = j;
    if (t == 0) break;
    now += t;
    v[p] = 1;
    gun = 1 - gun;
  }
  ans = max(ans, i - 1);
  cout << ans << endl;
}
int main() {
  Solve();
  cin >> n;
  return 0;
}
