#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
long long cdiv(long long a, long long b) {
  return a / b + ((a ^ b) > 0 && a % b);
}
long long fdiv(long long a, long long b) {
  return a / b - ((a ^ b) < 0 && a % b);
}
long long half(long long x) { return fdiv(x, 2); }
template <class T, class U>
T fstTrue(T lo, T hi, U first) {
  hi++;
  assert(lo <= hi);
  while (lo < hi) {
    T mid = half(lo + hi);
    first(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U first) {
  lo--;
  assert(lo <= hi);
  while (lo < hi) {
    T mid = half(lo + hi + 1);
    first(mid) ? lo = mid : hi = mid - 1;
  }
  return lo;
}
template <class T>
void remDup(vector<T>& v) {
  sort(begin(v), end(v));
  v.erase(unique(begin(v), end(v)), end(v));
}
template <class A>
void re(complex<A>& c);
template <class A, class B>
void re(pair<A, B>& p);
template <class A>
void re(vector<A>& v);
template <class A, size_t SZ>
void re(array<A, SZ>& a);
template <class T>
void re(T& x) {
  cin >> x;
}
void re(double& d) {
  string t;
  re(t);
  d = stod(t);
}
void re(long double& d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T>
void re(H& h, T&... t) {
  re(h);
  re(t...);
}
template <class A>
void re(complex<A>& c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B>
void re(pair<A, B>& p) {
  re(p.first, p.second);
}
template <class A>
void re(vector<A>& x) {
  for (auto& a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ>& x) {
  for (auto& a : x) re(a);
}
string to_string(char c) { return string(1, c); }
string to_string(const char* second) { return (string)second; }
string to_string(string second) { return second; }
string to_string(bool b) { return to_string((int)b); }
template <class A>
string to_string(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.string();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
  string res = "";
  for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
  bool fst = 1;
  string res = "";
  for (const auto& x : v) {
    if (!fst) res += " ";
    fst = 0;
    res += to_string(x);
  }
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
  return to_string(p.first) + " " + to_string(p.second);
}
template <class A>
void pr(A x) {
  cout << to_string(x);
}
template <class H, class... T>
void pr(const H& h, const T&... t) {
  pr(h);
  pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H& h, const T&... t) {
  pr(h);
  if (sizeof...(t)) pr(" ");
  ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long max(long long a, long long b, long long c) {
  return max(a, max(b, c));
}
int min(int a, int b, int c) { return min(a, min(b, c)); }
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(string second = "") {
  unsyncIO();
  if ((int)(second).size()) {
    setIn(second + ".in"), setOut(second + ".out");
  }
}
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  long long n, m;
  re(n, m);
  long long a[m], b[m];
  vector<int> v;
  for (int i = (0); i < (m); ++i) {
    re(a[i], b[i]);
    v.push_back(a[i]);
  }
  sort(begin(v), end(v));
  long long mx = 0;
  long long pre[m + 1];
  pre[0] = 0;
  for (int i = (0); i < (m); ++i) pre[i + 1] = pre[i] + v[i];
  for (int i = (0); i < (m); ++i) {
    long long cntleft = n - 1;
    long long ret = a[i];
    if (a[i] >= b[i]) {
      ret = 0;
      cntleft = n;
    }
    auto it = lower_bound(v.begin(), v.end(), b[i]);
    ret += pre[m] - pre[max(m - cntleft, (long long)distance(v.begin(), it))];
    cntleft -= distance(it, v.end());
    if (cntleft < 0) cntleft = 0;
    ret += cntleft * b[i];
    ckmax(mx, ret);
  }
  ps(mx);
}
vector<int> a, b, both;
int main() {
  setIO();
  int n, k;
  re(n, k);
  for (int i = (0); i < (n); ++i) {
    int x, y, z;
    re(x, y, z);
    if (y && z)
      both.push_back(x);
    else if (y)
      a.push_back(x);
    else if (z)
      b.push_back(x);
  }
  sort(begin(a), end(a));
  sort(begin(b), end(b));
  sort(begin(both), end(both));
  if (min((int)(a).size() + (int)(both).size(),
          (int)(b).size() + (int)(both).size()) < k) {
    ps(-1);
    return 0;
  }
  int mn = MOD;
  int sum = 0;
  int p = 0;
  int p2 = 0;
  while (p < (int)(both).size() && p < k) {
    sum += both[p];
    p++;
  }
  while (p + p2 < k) {
    sum += a[p2];
    sum += b[p2];
    p2++;
  }
  ckmin(mn, sum);
  while (p >= 0 && p2 < min((int)(a).size(), (int)(b).size())) {
    sum += a[p2];
    sum += b[p2];
    p2++;
    p--;
    sum -= both[p];
    ckmin(mn, sum);
  }
  assert(mn != MOD);
  ps(mn);
}
