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

public class Solution {

	static class Book {
		int t;
		int a, b;
		Book(int tt,int aa,int bb) {
			t = tt; a = aa; b = bb;
		}
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		F scn = new F(System.in);
		PrintWriter pw = new PrintWriter(System.out);
//		int t = scn.nextInt();
//		while(t-- > 0 ) {
			int n = scn.nextInt();
			int k = scn.nextInt();
			Book b[] = new Book[n];
			int ax = 0, bx = 0;
			for(int i=0;i<n;i++) {
				b[i] = new Book(scn.nextInt(), scn.nextInt(), scn.nextInt());
				if(b[i].a == 1) ax++;
				if(b[i].b == 1) bx++; 
			}
			
			if(ax < k || bx < k) {
				pw.println(-1);
			}
			else pw.println(ans(b,k));
//		}
		pw.flush();
	}
	
	public static long ans(Book[] a,int k) {
		Arrays.sort(a,new Comparator<Book>() {
			public int compare(Book a,Book b) {
				return a.t - b.t;
			}
		});
		
		int ax = 0, bx = 0;
		long ans = 0;
		boolean d[] = new boolean[a.length];
		for(int i=0;ax < k && i<a.length;i++) {
			if(a[i].a == 1) {
				if(a[i].b == 1) bx++;
				ax++;
				ans += a[i].t;
				d[i] = true;
			}
		}
		
		for(int i=0;i<a.length && bx < k;i++) {
			if(!d[i] && a[i].b == 1) {
				bx++;
				ans += a[i].t;
			}
		}
		
		return ans;
	}
	public static long ans(int a[],int k) {
		
		HashMap<Integer,Integer> m = new HashMap<>();
		long max = 0, mf = 0;
		for(int i : a) {
			int x = k - i%k;
			if(x != k) {
				m.put(x, m.getOrDefault(x, 0) + 1);
				if(mf < m.get(x)) {
					mf = m.get(x);
					max = x;
				}
				else if(mf == m.get(x) && x > max) {
					max = x;
				}
			}
		}
//		System.out.println(m);
		if(m.size() == 0) return 0; 
		return (mf-1)*k + max + 1L;
		
	}
	
	public static int ans(String s) {
		int c = 0;
		int min = Integer.MAX_VALUE;
		for(int i=0;i<s.length();i++) {
			if(s.charAt(i) == '(') c++;
			else c--;
			if(min > c) min = c;
		}
		
		return Math.abs(min);
	}
	public static int ans(int n) {
		int d[] = three(n);
		if(d[2] != 1) return -1;
		
		if(d[1] > d[0]) return -1;
		
		return d[0] + d[0] - d[1];
	}
	public static int[] three(int x) {
		int c3 = 0, c2 = 0;
		int y = x;
		while(x > 0 && x%3 == 0) {
			x /= 3;
			c3++;
		}
		
		while(x > 0 && x%2 == 0) {
			x /= 2;
			c2++;
		}
		
		return new int[] {c3,c2,x};
		
	}
	
	static class F {
		StringTokenizer st;
		BufferedReader br;

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

		public F(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 int[] nextIntArray(int n) throws IOException {
			int[] in = new int[n];
			for (int i = 0; i < n; i++)
				in[i] = nextInt();
			return in;
		}

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

		public int[] nextIntSortedArray(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			Arrays.sort(a);
			return a;
		}

		public long[] longSortedArr(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			Arrays.sort(a);
			return a;
		}

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

		public Long[] nextLongArray1(int n) throws IOException {
			Long[] in = new Long[n];
			for (int i = 0; i < n; i++)
				in[i] = nextLong();
			return in;
		}

		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 boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
		
		final static long M = 1000000007L;
		
		final static long power(long base, long exp) {
			long ans = 1;
			while(exp > 0) {
				if(exp%2 == 1) ans = (ans * base) % M; 
				exp/=2;
				base = (base * base) % M;
			}
			return ans;
		}
		
		final static long gcd(long a,long b) {
		    return b == 0 ? a : gcd(b, a%b);
		}
	}

}
