#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MOD2 = 1e9 + 9;
const long long MOD3 = 1e9 + 21;
const long long INF = 5e18;
const long double pi = acos(-1.0);
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long exp(long long a, long long b, long long M) {
  long long result = 1;
  while (b > 0) {
    if (b & 1) result = mod(result * a, M);
    b >>= 1;
    a = mod(a * a, M);
  }
  return result;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> alice;
  vector<long long> bob;
  vector<long long> ambos;
  long long al = 0, bb = 0;
  for (long long i = 0; i < n; i++) {
    long long x, a, b;
    cin >> x >> a >> b;
    if (a == 1 and b == 0) {
      alice.push_back(x);
      al++;
    } else if (a == 0 and b == 1) {
      bob.push_back(x);
      bb++;
    } else if (a == 1 and b == 1) {
      ambos.push_back(x);
      al++;
      bb++;
    }
  }
  sort(alice.begin(), alice.end());
  sort(bob.begin(), bob.end());
  sort(ambos.begin(), ambos.end());
  if (al < k or bb < k) {
    cout << -1 << endl;
    return;
  }
  while (true) {
    if (al > k and bb > k) {
      if (alice.size() > 0 and bob.size() > 0 and ambos.size() > 0) {
        if (alice[alice.size() - 1] >= bob[bob.size() - 1] and
            alice[alice.size() - 1] >= ambos[ambos.size() - 1]) {
          al--;
          alice.pop_back();
        } else if (bob[bob.size() - 1] >= alice[alice.size() - 1] and
                   bob[bob.size() - 1] >= ambos[ambos.size() - 1]) {
          bb--;
          bob.pop_back();
        } else if (ambos[ambos.size() - 1] >=
                   alice[alice.size() - 1] + bob[bob.size() - 1]) {
          al--;
          bb--;
          ambos.pop_back();
        } else {
          if (alice[alice.size() - 1] >= bob[bob.size() - 1]) {
            al--;
            alice.pop_back();
          } else {
            bb--;
            bob.pop_back();
          }
        }
      } else if (alice.size() > 0 and bob.size() == 0 and ambos.size() > 0) {
        if (alice[alice.size() - 1] >= ambos[ambos.size() - 1]) {
          al--;
          alice.pop_back();
        } else {
          al--;
          bb--;
          ambos.pop_back();
        }
      } else if (alice.size() > 0 and bob.size() > 0 and ambos.size() == 0) {
        al--;
        bb--;
        alice.pop_back();
        bob.pop_back();
      } else if (alice.size() == 0 and bob.size() > 0 and ambos.size() > 0) {
        if (bob[bob.size() - 1] >= ambos[ambos.size() - 1]) {
          bb--;
          bob.pop_back();
        } else {
          al--;
          bb--;
          ambos.pop_back();
        }
      } else if (alice.size() == 0 and bob.size() == 0 and ambos.size() > 0) {
        al--;
        bb--;
        ambos.pop_back();
      } else if (alice.size() == 0 and bob.size() > 0 and ambos.size() == 0) {
        bb--;
        bob.pop_back();
      } else if (alice.size() > 0 and bob.size() == 0 and ambos.size() == 0) {
        al--;
        alice.pop_back();
      }
    } else if (al > k) {
      if (alice.size() > 0) {
        al--;
        alice.pop_back();
      } else {
        break;
      }
    } else if (bb > k) {
      if (bob.size() > 0) {
        bb--;
        bob.pop_back();
      } else {
        break;
      }
    } else {
      break;
    }
  }
  long long ans = 0;
  for (long long &i : alice) {
    ans += i;
  }
  for (long long &i : bob) {
    ans += i;
  }
  for (long long &i : ambos) {
    ans += i;
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t = 1;
  while (t--) solve();
}
