/*        Author: thewackyindian		*/

 //:template starts here
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

#define fo(i,a,b)	for(ll i=a;i<b;i++)
#define foe(i,a,b)  	for(ll i=a;i<=b;i++) // 'e' in foen stands for equal
#define rev(i,b,a)  	for(ll i=b;i>=a;i--)//reverse where it starts from b and goes till a -> thus a<b.

#define read_arr(a,n) 	for(ll i=0;i<n;i++) cin>>a[i];
#define write_arr(a,n) 	for(auto &x:a){cout<<x<<" ";} cout<<endl;

#define vec    vector<ll>
#define vec2   vector<vector<ll>>
#define pr     pair<ll,ll>
#define pb	   push_back
#define in     insert
#define f 	   first
#define s 	   second

#define  fast_cin()    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define  mem(v,i)      memset(v,i,sizeof(v))
#define  all(x)        x.begin(), x.end()
#define  fps(x, y)     fixed << setprecision(y) << x
#define  endl          '\n'
#define  TIME          (chrono::steady_clock::now().time_since_epoch().count())

const long long mod = 1000000007;
// const long long  mod = 998244353;
const long long N = 3e5+5;
vec primes(ll n) { bool P[n+1] = {false}; vec p; for (ll j, i = 2; i <= n; i++) if (!P[i]) for (p.pb(i), j = i * i; j <= n; j += i) P[j] = true; return p; }
ll largest_bit(ll x) { return x==0 ? -1 : 63 - __builtin_clzll(x); }
ll gcd(ll a, ll b) { if(a < b) return gcd(b, a); if (!b) return a; return gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll add(ll a, ll b) { a %= mod; b %= mod; a = (a + b) % mod; return a; }
ll mul(ll a, ll b) { a %= mod; b %= mod; a = (a * b) % mod; return a; }
ll sub(ll a, ll b) { a %= mod; b %= mod; a = ((a - b) % mod + mod) % mod; return a; }
ll exp(ll x, ll y, ll m = mod) { ll res = 1; x = x % m; while (y > 0) { if (y & 1) res = (res * x) % m; y = y >> 1; x = (x * x) % m; } return res; }
ll modinv(ll x, ll m = mod) { return exp(x, m - 2, m); }
//:template ends here
bool comp(pair<ll,pair<ll,ll>>a,pair<ll,pair<ll,ll>>b)
{
		if(a.f<b.f && a.s.f+a.s.s==b.s.f+b.s.s)
			return true;
		else if(a.f>b.f)
			return false;
		return a.s.f+a.s.s >=b.s.f+b.s.s;
	
}
ll n,k;
struct book
{
	ll time;
	ll likea;
	ll likeb;
}book;
void itswacky()
{

	cin>>n>>k;
	vector<pair<ll,pair<ll,ll>>>details;
	ll la=0,lb=0;
	fo(i,0,n)
	{
		cin>>book.time>>book.likea>>book.likeb;
		if(book.likea!=0 || book.likeb!=0){
			details.pb({book.time,{book.likea,book.likeb}});
		}
		la+=book.likea;
		lb+=book.likeb;

	}
	if(la<k || lb<k){
		cout<<-1<<endl;
		return;
	}

	la=0;lb=0;
	ll sum=0;
	sort(all(details),comp);
	for(auto &x:details)
	{
		if(la==k && lb==k)
			break;
		if(x.s.f==1 && x.s.s==1){
			sum=sum+x.f;
			la++;
			lb++;
		}
		else if(x.s.f==0 && x.s.s==1){
			sum=sum+x.f;
			//la++;
			lb++;
		}
		else if(x.s.f==1 && x.s.s==0){
			sum=sum+x.f;
			la++;
			//lb++;
		}
	}
	if(la==k && lb==k){
		cout<<sum<<endl;
	}
		
}
int main()
{
	fast_cin();
	ll t=1;;
	//cin>>t;
	while(t--)
		itswacky();
	return 0;
}
/*if(la>=k && lb>=k)
			break;
		if(x.s.f==1 && x.s.s==1){
			sum=sum+x.f;
			la++;
			lb++;
		}
		else if(x.s.f==0 && x.s.s==1){
			sum=sum+x.f;
			//la++;
			lb++;
		}
		else if(x.s.f==1 && x.s.s==0){
			sum=sum+x.f;
			la++;
			//lb++;
		}*/