import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main {

	private static FastReader fr = new FastReader();
	private static Helper helper = new Helper();
	private static StringBuilder result = new StringBuilder();
 
	public static void main(String[] args) {
		Task solver = new Task();
		solver.solve();
	}
 
	static class Task {
	    
	    class Pair{
			public int t;
			public int ai;
			public int bi;
			public Pair(int t, int ai, int bi){
				this.t = t;
				this.ai = ai;
				this.bi = bi;
			}
			@Override
			public String toString() {
				return "Pair [t=" + t + ", ai=" + ai + ", bi=" + bi + "]";
			}
		}
	    
		public void solve() {
			int n = fr.ni(), k = fr.ni();
			ArrayList<Pair> books = new ArrayList<>();
			for(int i=0; i<n; i++) books.add(new Pair(fr.ni(), fr.ni(), fr.ni()));
			Collections.sort(books, new Comparator<Pair>(){
				@Override
				public int compare(Pair p1, Pair p2){
					if(p2.ai == 0 && p2.bi == 0) return -1;
					else return p1.t - p2.t;
				}
			});
//			System.out.println(books.toString());
			int countA = 0, countB = 0;
			long ans = 0;
			boolean isPossible = false;
			ArrayList<Pair> listA = new ArrayList<>();
			ArrayList<Pair> listB = new ArrayList<>();
			for(Pair book : books){
				if(book.ai == 1){
					if(countA < k){
						countA++;
						ans += book.t;
						if(book.bi == 1) countB++;
						else listA.add(book);
					}
					else{
						if(book.bi == 1){
							countA++;
							countB++;
							ans += book.t;
						}
					}
				}
				else if(book.bi == 1){
					if(countB < k){
						countB++;
						ans += book.t;
						if(book.ai == 1) countB++;
						else listB.add(book);
					}
				}
				if(countA >= k && countB >= k){
					isPossible = true;
					break;
				}
			}
			if(isPossible){
				for(int i=listB.size()-1; i>=0; i--){
					if(countB - 1 >= k){
						countB--;
						ans -= listB.get(i).t;
					}
					else break;
				}
				for(int i=listA.size()-1; i>=0; i--){
					if(countA - 1 >= k){
						countA--;
						ans -= listA.get(i).t;
					}
					else break;
				}
				System.out.println(ans);
			}
			else System.out.println(-1);
		}
	}
    
    static class Helper{
		public long[] tiArr(int n, int si){
			long[] arr = new long[n];
			for(int i=si; i<n; i++) arr[i] = fr.nl();
			return arr;
		}
	}
 
	static class FastReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		private static PrintWriter pw;
 
		public FastReader() {
			reader = new BufferedReader(new InputStreamReader(System.in));
			pw = new PrintWriter(System.out);
			tokenizer = null;
		}
 
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
 
		public int ni() {
			return Integer.parseInt(next());
		}
 
		public long nl() {
			return Long.parseLong(next());
		}
 
		public String rl() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
 
		public void print(String str) {
			pw.print(str);
			pw.flush();
		}
	}
}