#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> V1, V2, v1, v2;
int main() {
  int n, x, t, h, m, Max, Maxi, c, i, sum1 = 0, sum2 = 0, x1;
  pair<int, int> a;
  cin >> n >> x;
  x1 = x;
  for (i = 0; i < n; i++) {
    cin >> t >> h >> m;
    if (t) {
      a.first = h;
      a.second = m;
      v1.push_back(a);
    } else {
      a.first = h;
      a.second = m;
      v2.push_back(a);
    }
  }
  V1 = v1;
  V2 = v2;
  c = 1;
  while (1) {
    if (c > 0) {
      if (!V1.size()) break;
      Max = -1;
      for (i = 0; i < V1.size(); i++) {
        if (V1[i].first <= x && V1[i].second > Max) {
          Maxi = i;
          Max = V1[i].second;
        }
      }
      if (Max == -1) break;
      sum1++;
      x += V1[Maxi].second;
      a = V1[V1.size() - 1];
      V1[V1.size() - 1] = V1[Maxi];
      V1[Maxi] = a;
      V1.pop_back();
      c = -c;
    } else {
      if (!V2.size()) break;
      Max = -1;
      for (i = 0; i < V2.size(); i++) {
        if (V2[i].first <= x && V2[i].second > Max) {
          Maxi = i;
          Max = V2[i].second;
        }
      }
      if (Max == -1) break;
      sum1++;
      x += V2[Maxi].second;
      a = V2[V2.size() - 1];
      V2[V2.size() - 1] = V2[Maxi];
      V2[Maxi] = a;
      V2.pop_back();
      c = -c;
    }
  }
  c = -1;
  x = x1;
  while (1) {
    if (c > 0) {
      if (!v1.size()) break;
      Max = -1;
      for (i = 0; i < v1.size(); i++) {
        if (v1[i].first <= x && v1[i].second > Max) {
          Maxi = i;
          Max = v1[i].second;
        }
      }
      if (Max == -1) break;
      sum2++;
      x += v1[Maxi].second;
      a = v1[v1.size() - 1];
      v1[v1.size() - 1] = v1[Maxi];
      v1[Maxi] = a;
      v1.pop_back();
      c = -c;
    } else {
      if (!v2.size()) break;
      Max = -1;
      for (i = 0; i < v2.size(); i++) {
        if (v2[i].first <= x && v2[i].second > Max) {
          Maxi = i;
          Max = v2[i].second;
        }
      }
      if (Max == -1) break;
      sum2++;
      x += v2[Maxi].second;
      a = v2[v2.size() - 1];
      v2[v2.size() - 1] = v2[Maxi];
      v2[Maxi] = a;
      v2.pop_back();
      c = -c;
    }
  }
  cout << max(sum1, sum2);
  return 0;
}
