#include <bits/stdc++.h>
using namespace std;
int n, m, h, k, i, j, s2, H[2][3000], H2[2][3000], s, a, x, y, z, l[2], top;
vector<pair<int, int> > v[2];
int main() {
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> x >> y >> z;
    v[x].push_back(make_pair(z, y));
  }
  h = m;
  sort(v[0].begin(), v[0].end());
  sort(v[1].begin(), v[1].end());
  l[0] = v[0].size();
  l[1] = v[1].size();
  while (1) {
    a = 0;
    for (i = l[k] - 1; i >= 0; i--)
      if (v[k][i].second <= m && !H[k][i]) {
        s++;
        H[k][i] = 1;
        m += v[k][i].first;
        a = 1;
        break;
      }
    if (!a) break;
    k = !k;
  }
  m = h;
  k = 1;
  while (1) {
    a = 0;
    for (i = l[k] - 1; i >= 0; i--)
      if (v[k][i].second <= m && !H2[k][i]) {
        s2++;
        H2[k][i] = 1;
        m += v[k][i].first;
        a = 1;
        break;
      }
    if (!a) break;
    k = !k;
  }
  cout << max(s, s2);
}
