#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long n);
bool isprime(long long n);
struct node {
  long long a, b, t;
};
bool f(node n1, node n2) { return n1.t < n2.t; }
void test_case() {
  long long n, k;
  cin >> n >> k;
  node arr[n];
  for (long long i = 0; i < n; i++) cin >> arr[i].t >> arr[i].a >> arr[i].b;
  sort(arr, arr + n, f);
  long long time = 0, na = 0, nb = 0;
  for (long long i = 0; i < k; i++) {
    time += arr[i].t;
    na += arr[i].a;
    nb += arr[i].b;
  }
  long long i = k;
  while (i < n && (na < k || nb < k)) {
    na += arr[i].a;
    nb += arr[i].b;
    time += arr[i].t;
    i++;
  }
  i--;
  while (i >= 0) {
    if (na - arr[i].a >= k && nb - arr[i].b >= k) {
      na -= arr[i].a;
      nb -= arr[i].b;
      time -= arr[i].t;
    }
    i--;
  }
  if (na < k || nb < k)
    cout << "-1\n";
  else
    cout << time << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  test_case();
}
long long power(long long x, long long n) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) ans *= x;
    n >>= 1;
    x *= x;
  }
  return ans;
}
bool isprime(long long n) {
  if (n == 2 || n == 3) return true;
  if (n <= 1 || n % 2 == 0 || (n % 6 != 1 && n % 6 != 5)) return false;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return false;
  }
  return true;
}
