#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <class T>
inline T bigMod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T modInverse(T a, T M) {
  return bigMod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T>
inline string int2String(T a) {
  ostringstream str;
  str << a;
  return str.str();
}
const int maxn = 2e5 + 10;
struct info {
  int t, a, b;
} v[maxn];
int n, k, id[maxn];
bool cmp(const int& a, const int& b) {
  if (v[a].b == v[b].b) {
    return v[a].t < v[b].t;
  }
  return v[a].b > v[b].b;
}
std::priority_queue<int, std::vector<int>, decltype(&cmp)> pq(cmp);
bool cmp1(const int& a, const int& b) { return v[a].a > v[b].a; }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = int(0); i < int(n); i++) {
    scanf("%d%d%d", &v[i].t, &v[i].a, &v[i].b);
    id[i] = i;
  }
  sort(id, id + n, cmp1);
  long long sum = 0;
  int j, cnta = 0, cntb = 0;
  for (int i = int(0); i < int(k); i++) {
    j = id[i];
    pq.push(j);
    sum += v[j].t;
    cnta += v[j].a;
    cntb += v[j].b;
  }
  if (cnta < k) {
    puts("-1");
    return 0;
  }
  long long ans = 0x3f3f3f3f3f3f3f3f;
  if (cntb >= k) {
    ans = sum;
  };
  for (int i = int(k); i < int(n); i++) {
    ;
    j = id[i];
    pq.push(j);
    sum += v[j].t;
    cnta += v[j].a;
    cntb += v[j].b;
    ;
    while (cnta >= k && cntb >= k) {
      ans = min(ans, sum);
      j = pq.top();
      pq.pop();
      ;
      sum -= v[j].t;
      cnta -= v[j].a;
      cntb -= v[j].b;
    }
    cout << endl;
  }
  printf("%lld\n", ans >= 0x3f3f3f3f3f3f3f3f ? -1LL : ans);
  return 0;
}
