#include <bits/stdc++.h>
using namespace std;
vector<int> t, h, m, eaten;
int n, x;
int eat(int x, int inv) {
  for (int i = 0; i < n; ++i) eaten[i] = false;
  for (int i = inv; i < 2002; ++i) {
    int M = 0, J = -1;
    for (int j = 0; j < n; ++j)
      if (!eaten[j] && h[j] <= x && m[j] > M && ((i & 1) ? t[j] : !t[j]))
        M = m[j], J = j;
    if (!M) return i - inv;
    x += m[J];
    eaten[J] = true;
  }
  return 0;
}
int main() {
  cin >> n >> x;
  t.resize(n);
  h.resize(n);
  m.resize(n);
  eaten.resize(n);
  for (int i = 0; i < n; ++i) cin >> t[i] >> h[i] >> m[i];
  cout << max(eat(x, 0), eat(x, 1));
  return 0;
}
