#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long T;
  T = 1;
  while (T--) {
    long long n, k;
    cin >> n >> k;
    vector<pair<long long, pair<long long, long long>>> v;
    for (long long i = 0; i < n; i++) {
      long long t, a, b;
      cin >> t >> a >> b;
      v.emplace_back(t, make_pair(a, b));
    }
    sort(v.begin(), v.end());
    vector<long long> p(2, 0);
    vector<long long> chosen(n, 0);
    long long last = -1;
    for (long long i = 0; i < n; i++) {
      p[0] += v[i].second.first;
      p[1] += v[i].second.second;
      chosen[i] = 1;
      last = i;
    }
    for (long long a = last; a >= 0; a--) {
      if ((v[a].second.first != v[a].second.second) ||
          v[a].second.second == 0) {
        if (p[0] - v[a].second.first >= k && p[1] - v[a].second.second >= k) {
          chosen[a] = 0;
          p[0] -= v[a].second.first;
          p[1] -= v[a].second.second;
        }
      }
    }
    for (long long a = last; a >= 0; a--) {
      if ((v[a].second.first == v[a].second.second)) {
        if (p[0] - v[a].second.first >= k && p[1] - v[a].second.second >= k) {
          chosen[a] = 0;
          p[0] -= v[a].second.first;
          p[1] -= v[a].second.second;
        }
      }
    }
    if (p[0] < k || p[1] < k) {
      cout << -1 << endl;
      return 0;
    }
    long long ans = 0;
    for (long long i = 0; i <= last; i++) {
      if (chosen[i]) ans += v[i].first;
    }
    cout << ans << endl;
  }
}
