#include <bits/stdc++.h>
using namespace std;
void ac(long long x) {
  if (x)
    puts("YES");
  else
    puts("NO");
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long ppow(long long a, long long b, long long mod) {
  a %= mod;
  long long ans = 1 % mod;
  while (b) {
    if (b & 1) ans = (long long)ans * a % mod;
    a = (long long)a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long readdd() {
  long long x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return f * x;
}
void printtt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) printtt(x / 10);
  putchar(x % 10 + '0');
}
bool addd(long long a, long long b) { return a > b; }
long long lowbit(long long x) { return x & -x; }
const long long dx[4] = {0, 0, 1, -1};
const long long dy[4] = {1, -1, 0, 0};
const long long BN = 1e5 + 5;
struct BIT {
  long long c[BN];
  void init() { memset(c, 0, sizeof c); }
  long long lowbit(long long i) { return i & -i; }
  void add(long long i, long long t) {
    while (i < BN) c[i] += t, i += lowbit(i);
  }
  long long ask(long long i) {
    long long ans = 0;
    while (i) ans += c[i], i -= lowbit(i);
    return ans;
  }
};
bool Isprime(long long x) {
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) return 0;
  return 1;
}
void init() { ; }
const double Pi = acos(-1);
const double eps = 1e-7;
const long long mod = 1e9 + 7;
const long long modd = mod - 1;
const long long N = 2e5 + 5;
long long a[N], b[N];
long long d[N];
long long n, m;
struct Node {
  long long a, b, c;
} e[N];
bool cmp(Node a, Node b) {
  if (a.a != b.a) return a.a < b.a;
  if (a.b && a.c) {
    return 1;
  }
  return 0;
}
void solve() {
  long long n = readdd(), k = readdd();
  for (signed long long(i) = 1; (i) <= (n); (i)++) {
    e[i] = {readdd(), readdd(), readdd()};
  }
  sort(e + 1, e + 1 + n, cmp);
  long long c = 0, cc = 0;
  long long ans = 0;
  priority_queue<long long, vector<long long>, less<long long> > q, qq;
  for (signed long long(i) = 1; (i) <= (n); (i)++) {
    if (e[i].b && e[i].c) {
      if (c < k || cc < k) {
        c++, cc++;
        ans += e[i].a;
      } else {
        if (q.size() && qq.size()) {
          long long x = q.top();
          long long y = qq.top();
          if (e[i].a < x + y) {
            q.pop();
            qq.pop();
            ans -= x + y - e[i].a;
          }
        }
      }
    } else {
      if (e[i].b) {
        if (c < k) {
          c++;
          ans += e[i].a;
          q.push(e[i].a);
        }
      } else {
        if (cc < k) {
          cc++;
          ans += e[i].a;
          qq.push(e[i].a);
        }
      }
    }
  }
  if (c < k || cc < k) {
    printtt(-1);
    puts("");
    ;
    return;
  }
  while (c > k && q.size()) {
    ans -= q.top();
    q.pop();
    c--;
  }
  while (cc > k && qq.size()) {
    ans -= qq.top();
    q.pop();
    cc--;
  }
  printtt(ans);
  puts("");
  ;
}
signed main() {
  init();
  solve();
  return 0;
}
