#include <bits/stdc++.h>
using namespace std;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long exp(long long a, long long b, long long m) {
  if (b == 0) return 1;
  if (b == 1) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
vector<pair<long long, long long> > v1, v2;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, x;
  cin >> n >> x;
  for (long long i = 0; i < n; i++) {
    long long t, h, m;
    cin >> t >> h >> m;
    if (t == 0) {
      v1.push_back(pair<long long, long long>(h, m));
    } else
      v2.push_back(pair<long long, long long>(h, m));
  }
  long long cur = 0;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  long long n1 = v1.size();
  long long n2 = v2.size();
  long long i = 0, j = 0;
  priority_queue<long long> q1, q2;
  long long t = 0;
  long long X = x;
  while (cur < n) {
    if (t == 0) {
      while (i < n1 && x >= v1[i].first) {
        q1.push(v1[i].second);
        i++;
      }
      if (q1.empty()) {
        break;
      } else {
        x += q1.top();
        q1.pop();
        cur++;
      }
    } else {
      while (j < n2 && x >= v2[j].first) {
        q2.push(v2[j].second);
        j++;
      }
      if (q2.empty()) {
        break;
      } else {
        x += q2.top();
        q2.pop();
        cur++;
      }
    }
    t ^= 1;
  }
  long long ans = cur;
  cur = 0;
  i = 0, j = 0;
  priority_queue<long long> q3, q4;
  q1 = q3;
  q2 = q4;
  t = 1;
  x = X;
  while (cur < n) {
    if (t == 0) {
      while (i < n1 && x >= v1[i].first) {
        q1.push(v1[i].second);
        i++;
      }
      if (q1.empty()) {
        break;
      } else {
        x += q1.top();
        q1.pop();
        cur++;
      }
    } else {
      while (j < n2 && x >= v2[j].first) {
        q2.push(v2[j].second);
        j++;
      }
      if (q2.empty()) {
        break;
      } else {
        x += q2.top();
        q2.pop();
        cur++;
      }
    }
    t ^= 1;
  }
  ans = max(ans, cur);
  cout << ans << "\n";
}
