#include <bits/stdc++.h>
using namespace std;
int n, x, a, b, c, ans1, ans2, m1, m2, x1 = -1, x2 = -1, h1, h2, o;
bool was1[4000], was2[4000], c1;
vector<pair<int, int> > k, l;
int main() {
  cin >> n >> x;
  h1 = h2 = x;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    if (a == 0) {
      k.push_back(make_pair(c, b));
    }
    if (a == 1) {
      l.push_back(make_pair(c, b));
    }
  }
  sort(k.begin(), k.end());
  sort(l.begin(), l.end());
  for (int i = 0; i < k.size(); i++) {
    if (h1 >= k[i].second) {
      if (m1 < k[i].first) {
        m1 = k[i].first;
        x1 = i;
      }
    }
  }
  if (x1 >= 0) {
    ans1++;
    h1 += m1;
    was1[x1] = 1;
    o = 0;
    m1 = 0;
    x1 = -1;
    c1 = 1;
  }
  while (c1) {
    c1 = 0;
    if (o == 1) {
      for (int i = 0; i < k.size(); i++) {
        if (h1 >= k[i].second && !was1[i]) {
          if (m1 < k[i].first) {
            m1 = k[i].first;
            x1 = i;
          }
        }
      }
      if (x1 >= 0) {
        c1 = 1;
        was1[x1] = 1;
        h1 += m1;
        ans1++;
        m1 = 0;
        x1 = -1;
        o = 0;
      }
    } else if (o == 0) {
      for (int i = 0; i < l.size(); i++) {
        if (h1 >= l[i].second && !was2[i]) {
          if (m1 < l[i].first) {
            m1 = l[i].first;
            x1 = i;
          }
        }
      }
      if (x1 >= 0) {
        c1 = 1;
        was2[x1] = 1;
        h1 += m1;
        ans1++;
        m1 = 0;
        x1 = -1;
        o = 1;
      }
    }
  }
  for (int i = 0; i < k.size(); i++) {
    was1[i] = 0;
  }
  for (int i = 0; i < l.size(); i++) {
    was2[i] = 0;
  }
  for (int i = 0; i < l.size(); i++) {
    if (h2 >= l[i].second) {
      if (m2 < l[i].first) {
        m2 = l[i].first;
        x2 = i;
      }
    }
  }
  if (x2 >= 0) {
    ans2++;
    h2 += m2;
    was2[x2] = 1;
    o = 1;
    m2 = 0;
    x2 = -1;
    c1 = 1;
  }
  while (c1) {
    c1 = 0;
    if (o == 1) {
      for (int i = 0; i < k.size(); i++) {
        if (h2 >= k[i].second && !was1[i]) {
          if (m2 < k[i].first) {
            m2 = k[i].first;
            x2 = i;
          }
        }
      }
      if (x2 >= 0) {
        c1 = 1;
        was1[x2] = 1;
        h2 += m2;
        ans2++;
        m2 = 0;
        x2 = -1;
        o = 0;
      }
    } else if (o == 0) {
      for (int i = 0; i < l.size(); i++) {
        if (h2 >= l[i].second && !was2[i]) {
          if (m2 < l[i].first) {
            m2 = l[i].first;
            x2 = i;
          }
        }
      }
      if (x2 >= 0) {
        c1 = 1;
        was2[x2] = 1;
        h2 += m2;
        ans2++;
        m2 = 0;
        x2 = -1;
        o = 1;
      }
    }
  }
  cout << max(ans1, ans2);
}
