
import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class test1{

	static List<Integer> merge(List<Integer> list1, List<Integer> list2){
		List<Integer> temp = new ArrayList<>();
		int i=0, j=0;
		for(; i<list1.size() && j<list2.size(); ){
			if(list1.get(i) < list2.get(j)){
				temp.add(list1.get(i++));
			}
			else{
				temp.add(list2.get(j++));
			}
		}

		while(!list1.isEmpty()){
			temp.add(list1.get(i));
		}
		while(!list2.isEmpty()){
			temp.add(list2.get(i));
		}
		return temp;
	}

    public static void main(String[] args) throws IOException{
        long startTime = System.currentTimeMillis();
        Reader sc = new Reader();
		// int t = sc.nextInt();
		StringBuilder finalResult= new StringBuilder();
		// int count=0;
        // while(t-- > 0){
			int n=sc.nextInt();
			int k=sc.nextInt();
			int[] arr=new int[n];
			List<Integer> a = new ArrayList<>();
			List<Integer> b = new ArrayList<>();
			List<Integer> c = new ArrayList<>();
			for(int i=0; i<n; i++){
				arr[i]=sc.nextInt();
				int al=sc.nextInt();
				int bob=sc.nextInt();
				if(al == 1 && bob == 1)
					c.add(arr[i]);
				else if(al == 1)
					a.add(arr[i]);
				else if(bob == 1)
					b.add(arr[i]);
			}

			Collections.sort(a);
			Collections.sort(b);
			Collections.sort(c);

			// int i=0, j=0;
			// while(i<a.size() && j<b.size()){
			// 	c.add(a.get(i)+b.get(j));
			// }

			// if(c.size() < k)
			// 	result=-1;
			// else{
			// 	for(i=0; i<k; i++){
			// 		result += c.get(i);
			// 	}
			// }

			
			int nal = a.size(), nbl = b.size(), ncl = c.size();
			long result=0;
			if(nal+ncl < k || nbl+ncl < k)
				result=-1;
			else{
				
				
				int reqa=k, reqb=k;
				int biggerReq = k-nal < k-nbl ? k-nbl : k-nal;
				int cIndex=0;
// System.out.println("bigreq "+biggerReq);
				for(cIndex=0; cIndex<biggerReq; cIndex++){
					result += c.get(cIndex);
					reqa--;
					reqb--;
					// System.out.println("reqa -"+reqa);
				}

				int aIndex=a.size()-1 , bIndex=b.size()-1;
				while(cIndex<ncl && aIndex>0 && bIndex>0 && c.get(cIndex) < (a.get(aIndex)+b.get(bIndex))){
					result += c.get(cIndex);
					reqa--;
					reqb--;
					aIndex--;
					bIndex--;
					cIndex++;
				}
// System.out.println("reqa "+reqa);
				for(int i=0; i<reqa; i++){
					result += a.get(i);
				}

				for(int i=0; i<reqb; i++){
					result += b.get(i);
				}
			
			}
			
			finalResult.append(result);
		// }   
		System.out.print(finalResult);
        long endTime = System.currentTimeMillis();
    }

    static class Reader {
	    final private int BUFFER_SIZE = 1 << 16;
	    private DataInputStream din;
	    private byte[] buffer;
	    private int bufferPointer, bytesRead;

	    public Reader() {
	      din = new DataInputStream(System.in);
	      buffer = new byte[BUFFER_SIZE];
	      bufferPointer = bytesRead = 0;
	    }

	    public Reader(String file_name) throws IOException {
	      din = new DataInputStream(new FileInputStream(file_name));
	      buffer = new byte[BUFFER_SIZE];
	      bufferPointer = bytesRead = 0;
	    }

	    public String readLine() throws IOException {
	      byte[] buf = new byte[200002];
	      int cnt = 0, c;
	      while ((c = read()) != -1) {
	        if (c == '\n') break;
	        buf[cnt++] = (byte) c;
	      }
	      return new String(buf, 0, cnt);
	    }

	    public int nextInt() throws IOException {
	      int ret = 0;
	      byte c = read();
	      while (c <= ' ') c = read();
	      boolean neg = (c == '-');
	      if (neg) c = read();
	      do {
	        ret = ret * 10 + c - '0';
	      } while ((c = read()) >= '0' && c <= '9');
	      if (neg) return -ret;
	      return ret;
	    }

	    public long nextLong() throws IOException {
	      long ret = 0;
	      byte c = read();
	      while (c <= ' ') c = read();
	      boolean neg = (c == '-');
	      if (neg) c = read();
	      do {
	        ret = ret * 10 + c - '0';
	      } while ((c = read()) >= '0' && c <= '9');
	      if (neg) return -ret;
	      return ret;
	    }

	    public double nextDouble() throws IOException {
	      double ret = 0, div = 1;
	      byte c = read();
	      while (c <= ' ') c = read();
	      boolean neg = (c == '-');
	      if (neg) c = read();
	      do {
	        ret = ret * 10 + c - '0';
	      } while ((c = read()) >= '0' && c <= '9');
	      if (c == '.')
	        while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
	      if (neg) return -ret;
	      return ret;
	    }

	    private void fillBuffer() throws IOException {
	      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
	      if (bytesRead == -1) buffer[0] = -1;
	    }

	    private byte read() throws IOException {
	      if (bufferPointer == bytesRead) fillBuffer();
	      return buffer[bufferPointer++];
	    }

	    public void close() throws IOException {
	      if (din == null) return;
	      din.close();
	    }
	  }
}

class Pair{
	int x,y;
	Pair(int a, int b){
		x=a;y=b;
	}
}