#include<bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
 
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
// name.order_of_key(val): returns the no. of values strictly less than val
// *name.find_by_order(k): returns to an iterator to the kth element (counting from zero) 
 
#define scl(x) scanf("%lld",&x)
#define sc(x) scanf("%d",&x)
#define prl(x) printf("%lld ",x)
#define pr(x) printf("%d ",x)
#define nl printf("\n")
#define vi vector<int>
#define vii vector<long long int>
#define mkp make_pair
#define pb push_back
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define mset(x,y) memset(x,y,sizeof(x))
#define rep(i,a,b) for(int i=a;i<b;i++)
const int N=(int)1e6+5;
const int mod = 1000000007;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
 
#define TRACE
 
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
 
// printf("%0.12Lf\n", ans);
// std::cout << std::setprecision(9) << ans << endl;
// vector <int>::iterator it = find (all(v1),y);
// vector<int>::iterator itr=lower_bound(v.begin(),v.end(),x);
// cout << "index: "<< itr-v.begin() <<" value : "<< *itr <<endl;
// map<int,int>::iterator it2=mp.find(y);
// map<int,int>::iterator it=mp.begin();
// for (;it!=mp.end();it++)
// mp[it->fi]=it->se ;
vi v1,v2,diff;
int a[N], b[N], t[N];
map<int,int> mp;
int main()
{
 	std::ios::sync_with_stdio(false);cin.tie(NULL) ;
 	int n,m,k;
 	cin >> n >> k;

 	set<int> both, alice, bob;
 	rep (i,1,n+1)
 	{
 		cin >> t[i] >> a[i] >> b[i];
 		
 		if (a[i]+b[i]==2) both.insert(t[i]);
 		else if (a[i]==1) alice.insert(t[i]);
 		else if (b[i]==1) bob.insert(t[i]);
 	}

 	ll ans=0;
 	while (k>0)
 	{
 		// trace(sz(both), sz(alice), sz(bob));
 		if (sz(both)==0)
 		{
 			if (sz(alice)>0 && sz(bob)>0){
		 		auto a=alice.begin();
		 		auto b=bob.begin();
	 			ans += (*a + *b);
	 			alice.erase(a);
	 			bob.erase(b);
	 			k--;
	 			// trace(sz(alice), sz(bob)); 			
	 		}
	 		else break;
 		}
 		else if (sz(both)>0 && (sz(alice)==0 || sz(bob)==0))
 		{
	 		auto x=both.begin();
 			ans += *x;
 			both.erase(x);
 			k--;
 		}
 		else{
	 		auto a=alice.begin();
	 		auto b=bob.begin();
	 		auto x=both.begin();
	 		if (*a + *b <= *x)
	 		{
	 			ans += (*a + *b);
	 			alice.erase(a);
	 			bob.erase(b);
	 			k--;
	 		}
	 		else
	 		{
	 			ans += *x;
	 			both.erase(x);
	 			k--;
	 		}
 		}
 	}
 	if (k>0) cout << -1 << endl;
 	else cout << ans << endl;
    return 0 ;
}