#include <bits/stdc++.h>
int a[2000][3];
using namespace std;
int check(int m, int x, int n) {
  int c = -1;
  for (int i = 0; i < n; i++) {
    if (a[i][0] == m && a[i][1] <= x && a[i][1] != -1) {
      if (c == -1) {
        c = i;
      } else if (a[i][2] > a[c][2]) {
        c = i;
      }
    }
  }
  return c;
}
int main() {
  int n, x, na = 0, nb = 0, tx;
  cin >> n >> x;
  tx = x;
  int y[n][3];
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
    y[i][0] = a[i][0];
    y[i][1] = a[i][1];
    y[i][2] = a[i][2];
  }
  int m = 0, c = 0, d;
  while ((d = check(m, x, n)) != -1) {
    x += a[d][2];
    a[d][1] = -1;
    c++;
    if (m == 0)
      m = 1;
    else
      m = 0;
  }
  int max = c;
  c = 0;
  for (int i = 0; i < n; i++) {
    a[i][0] = y[i][0];
    a[i][1] = y[i][1];
    a[i][2] = y[i][2];
  }
  m = 1;
  x = tx;
  while ((d = check(m, x, n)) != -1) {
    x += a[d][2];
    a[d][1] = -1;
    c++;
    if (m == 0)
      m = 1;
    else
      m = 0;
  }
  if (c > max)
    cout << c;
  else
    cout << max;
  return 2048 - 1024 - 1024;
}
