#include <bits/stdc++.h>
using namespace std;
const int MAXN = 205;
const long long int inf = 1e12;
const long long int mod = 1000000007;
long long int gc(long long int a, long long int b) {
  if (b == 0) return a;
  return gc(b, a % b);
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<bool> prime(1000005, true);
vector<long long int> v;
void sieve(long long int n) {
  prime[1] = false;
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (prime[i] == true) v.push_back(i);
  }
}
inline long long int add(long long int a, long long int b) {
  return ((a % mod) + (b % mod)) % mod;
}
inline long long int sub(long long int a, long long int b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
inline long long int mul(long long int a, long long int b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long int recur(long long int idx, long long int sum,
                    vector<pair<long long int, long long int> >& v,
                    vector<long long int>& b, vector<long long int>& pre,
                    vector<long long int>& dp) {
  if (idx == b.size()) return 0;
  if (dp[idx] != -1) return dp[idx];
  long long int temp = INT_MAX;
  temp = min(temp, recur(idx + 1, sum, v, b, pre, dp) + b[idx]);
  for (int i = 0; i < v.size(); i++) {
    long long int x = v[i].first;
    long long int y = v[i].second;
    if (idx + x - 1 >= b.size()) continue;
    long long int calc = pre[idx + x - 1] - pre[idx + y - 1];
    temp = min(temp, recur(idx + x, sum, v, b, pre, dp) + calc);
  }
  dp[idx] = temp;
  return temp;
}
long long int dfs(long long int src, long long int p, vector<long long int>& a,
                  vector<pair<long long int, long long int> > adj[],
                  vector<long long int>& dp) {
  long long int can1 = 0;
  long long int can2 = 0;
  vector<pair<long long int, long long int> >::iterator it;
  for (it = adj[src].begin(); it != adj[src].end(); it++) {
    if (((*it).first == p)) continue;
    long long int st =
        max(0LL, dfs((*it).first, src, a, adj, dp) - (*it).second);
    if (st > can1) {
      can2 = can1;
      can1 = st;
    } else if (st > can2) {
      can2 = st;
    }
  }
  dp[src] = a[src] + can1 + can2;
  return (max(can1, can2) + a[src]);
}
struct comp1 {
  bool operator()(pair<long long int, long long int>& temp1,
                  pair<long long int, long long int>& temp2) {}
};
bool comp2(pair<long long int, long long int>& temp1,
           pair<long long int, long long int>& temp2) {
  return ((temp1.first < temp2.first) ||
          ((temp1.first == temp2.first) && (temp1.second < temp2.second)));
}
long long int getinv(long long int temp) { return power(temp, mod - 2, mod); }
void computeLPSArray(string pat, long long int M, vector<long long int>& lps) {
  int len = 0;
  lps[0] = 0;
  long long int i = 1;
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else {
      if (len != 0) {
        len = lps[len - 1];
      } else {
        lps[i] = 0;
        i++;
      }
    }
  }
}
long long int fin(long long int u, vector<long long int>& parent) {
  if (u == parent[u])
    return u;
  else
    return parent[u] = fin(parent[u], parent);
}
void unio(long long int u, long long int v, vector<long long int>& parent,
          vector<long long int>& ran) {
  u = fin(u, parent);
  v = fin(v, parent);
  if (u == v) return;
  if (ran[u] >= ran[v]) {
    parent[v] = u;
    ran[u] += ran[v];
  } else {
    parent[u] = v;
    ran[v] += ran[u];
  }
}
void update(int x, int delta, vector<long long int>& BIT) {
  long long int n = BIT.size() - 1;
  for (; x <= n; x += x & -x) BIT[x] += delta;
}
int query(int x, vector<long long int>& BIT) {
  int sum = 0;
  for (; x > 0; x -= x & -x) sum += BIT[x];
  return sum;
}
int main() {
  long long int n, k;
  cin >> n >> k;
  vector<long long int> s;
  vector<long long int> s1;
  vector<long long int> s2;
  for (long long int i = 0; i < n; i++) {
    long long int t1, t2, t3;
    cin >> t1 >> t2 >> t3;
    if (t2 == 1 && t3 == 1) {
      s.push_back(t1);
      continue;
    }
    if (t2 == 0 && t3 == 0) continue;
    if (t2 == 1) s1.push_back(t1);
    if (t3 == 1) s2.push_back(t1);
  }
  if (((int)s.size() + (int)s1.size() < k) ||
      ((int)s.size() + (int)s2.size() < k)) {
    cout << -1;
    return 0;
  }
  long long int c1 = 0;
  long long int c2 = 0;
  long long int idx1 = 0;
  long long int idx2 = 0;
  long long int idx3 = 0;
  long long int ans = 0;
  sort(s.begin(), s.end());
  sort(s1.begin(), s1.end());
  sort(s2.begin(), s2.end());
  while (idx1 < s.size() && idx2 < s1.size() && idx3 < s2.size()) {
    if (c1 >= k && c2 >= k) {
      break;
    }
    long long int a1 = s[idx1];
    long long int a2 = s1[idx2];
    long long int a3 = s2[idx3];
    if (a2 + a3 < a1) {
      ans += (a2 + a3);
      c1++;
      c2++;
      idx2++;
      idx3++;
    } else {
      ans += a1;
      c1++;
      c2++;
      idx1++;
    }
  }
  if (c1 >= k && c2 >= k) {
    cout << ans << endl;
    return 0;
  }
  if (idx1 == s.size()) {
    for (long long int i = idx2; i < s1.size() && c1 < k; i++) {
      ans += s1[idx2];
      c1++;
    }
    for (long long int i = idx3; i < s2.size() && c2 < k; i++) {
      ans += s2[idx3];
      c2++;
    }
    cout << ans << endl;
    return 0;
  }
  if (idx2 == s1.size()) {
    long long int i;
    for (i = idx1; i < s.size() && c1 < k; i++) {
      ans += s[i];
      c1++;
      c2++;
    }
    vector<long long int> v;
    for (; i < s.size(); i++) {
      v.push_back(s[i]);
    }
    for (i = idx3; i < s2.size(); i++) {
      v.push_back(s2[i]);
    }
    sort(v.begin(), v.end());
    for (i = 0; i < v.size() && c2 < k; i++) {
      c2++;
      ans += v[i];
    }
    cout << ans << endl;
    return 0;
  }
  if (idx3 == s2.size()) {
    long long int i;
    for (i = idx1; i < s.size() && c2 < k; i++) {
      ans += s[i];
      c1++;
      c2++;
    }
    vector<long long int> v;
    for (; i < s.size(); i++) {
      v.push_back(s[i]);
    }
    for (i = idx2; i < s1.size(); i++) {
      v.push_back(s1[i]);
    }
    sort(v.begin(), v.end());
    for (i = 0; i < v.size() && c1 < k; i++) {
      c1++;
      ans += v[i];
    }
    cout << ans << endl;
    return 0;
  }
}
