import java.util.*;
import java.io.*;
import java.util.Map.*;

public class E1_1374 {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		
		int n = sc.nextInt(), k = sc.nextInt();
		ArrayList<Integer> a = new ArrayList<>(), b = new ArrayList<>(), c = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			int t = sc.nextInt(), u = sc.nextInt(), v = sc.nextInt();
			if(u == v) {
				if(u == 1) {
					c.add(t);
				}
			} else {
				if(u == 1) {
					a.add(t);
				} else {
					b.add(t);
				}
			}
		}
		
		Collections.sort(a);
		Collections.sort(b);
		Collections.sort(c);
		
		if(Math.min(a.size(), b.size()) + c.size() < k)
			pw.println(-1);
		else {
			long cnt = 0;
			int i = 0, j = 0;
			int u = 0;
			while(u < k) {
				if(i == a.size() || i == b.size()) {
					cnt += c.get(j);
					j++;
				} else if(j == c.size()) {
					cnt += a.get(i) + b.get(i);
				} else {
					int s = a.get(i) + b.get(i), f = c.get(j);
					if(s <= f) {
						cnt += s;
						i++;
					} else {
						cnt += f;
						j++;
					}
				}
				u++;
			}
			pw.println(cnt);
		}
		
		pw.flush();
	}

	public static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		public Integer[] nextIntegerArray(int n) throws IOException {
			Integer[] array = new Integer[n];
			for (int i = 0; i < n; i++)
				array[i] = new Integer(nextInt());
			return array;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

		public double[] nextDoubleArray(int n) throws IOException {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		public static int[] shuffle(int[] a) {
			int n = a.length;
			Random rand = new Random();
			for (int i = 0; i < n; i++) {
				int tmpIdx = rand.nextInt(n);
				int tmp = a[i];
				a[i] = a[tmpIdx];
				a[tmpIdx] = tmp;
			}
			return a;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}
