#include <bits/stdc++.h>
struct node {
  int x, y;
} a[2005], b[2005], t1[2005], t2[2005];
int main() {
  int n, x;
  while (scanf("%d%d", &n, &x) != EOF) {
    int k = x;
    int k1, k2;
    k1 = k2 = 0;
    for (int i = 1; i <= n; i++) {
      int k, x, y;
      scanf("%d%d%d", &k, &x, &y);
      if (k == 0) {
        a[k1].x = x;
        a[k1++].y = y;
      } else {
        b[k2].x = x;
        b[k2++].y = y;
      }
    }
    for (int i = 0; i < k1; i++) t1[i] = a[i];
    for (int j = 0; j < k2; j++) t2[j] = b[j];
    int count = 0;
    int flag = 0;
    while (1) {
      if (flag == 0) {
        int max = 0;
        int id = -1;
        for (int i = 0; i < k1; i++) {
          if (a[i].x <= x && a[i].y >= max) {
            max = a[i].y;
            id = i;
          }
        }
        if (id == -1) break;
        x += a[id].y;
        a[id].x = 1000000005;
        count++;
      } else {
        int max = 0;
        int id = -1;
        for (int j = 0; j < k2; j++) {
          if (b[j].x <= x && b[j].y >= max) {
            max = b[j].y;
            id = j;
          }
        }
        if (id == -1) break;
        x += b[id].y;
        b[id].x = 1000000005;
        count++;
      }
      flag = (flag + 1) % 2;
    }
    int cnt = 0;
    flag = 1;
    x = k;
    for (int i = 0; i < k1; i++) a[i] = t1[i];
    for (int j = 0; j < k2; j++) b[j] = t2[j];
    while (1) {
      if (flag == 0) {
        int max = 0;
        int id = -1;
        for (int i = 0; i < k1; i++) {
          if (a[i].x <= x && a[i].y >= max) {
            max = a[i].y;
            id = i;
          }
        }
        if (id == -1) break;
        x += a[id].y;
        a[id].x = 1000000005;
        cnt++;
      } else {
        int max = 0;
        int id = -1;
        for (int j = 0; j < k2; j++) {
          if (b[j].x <= x && b[j].y >= max) {
            max = b[j].y;
            id = j;
          }
        }
        if (id == -1) break;
        x += b[id].y;
        b[id].x = 1000000005;
        cnt++;
      }
      flag = (flag + 1) % 2;
    }
    if (cnt > count) count = cnt;
    printf("%d\n", count);
  }
  return 0;
}
