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

public class Solution {

    static boolean found = false;
    static String ans = "";
    public static void main(String[] args) throws IOException {
        FastScanner fs=new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        // int T=fs.nextInt();
        // for (int tt=0; tt<T; tt++) {
        // }

        int n  = fs.nextInt();
        int k  = fs.nextInt();
        int[][] book = new int[n][3];
        for (int i=0; i<n; i++) {
            book[i][0] = fs.nextInt();
            book[i][1] = fs.nextInt();
            book[i][2] = fs.nextInt();
        }
        Arrays.sort(book, (a,b)->a[0]-b[0]);

        PriorityQueue<Integer> pqa = new PriorityQueue<Integer>((a,b)->(b-a));
        PriorityQueue<Integer> pqb = new PriorityQueue<Integer>((a,b)->(b-a));
        PriorityQueue<Integer> pqc = new PriorityQueue<Integer>((a,b)->(b-a));

        long time = 0, a= 0, b=0;
        for (int i=0; i<n; i++) {
            if (book[i][1]==0 && book[i][2]==0) {
                continue;
            }
            
            if (book[i][1]==1 && book[i][2]==1) {
                time+=book[i][0];
                pqc.offer(book[i][0]);

                a++;b++;
            }
            else if (book[i][1]==1){
                time+=book[i][0];
                pqa.offer(book[i][0]);
                a++;
            }
            else {
                time+=book[i][0];
                pqb.offer(book[i][0]);
                b++;
            }
            if (a>=k && b>=k) {
                break;
            }
        }

        while (a>k && b>k) {
            if (pqc.peek()>pqa.peek() && pqc.peek()>pqb.peek()) {
                time-=pqc.poll();
                a--;
                b--;
            }
            else if(pqa.peek()>pqb.peek()){
                time-=pqa.poll();
                a--;
            }
            else {
                time-=pqb.poll();
                b--;
            }
        }

        while (a>k && !pqa.isEmpty()) {
            time-=pqa.poll();
            a--;
        }
        while (b>k && !pqb.isEmpty()) {
            time-=pqb.poll();
            b--;
        }


        if (a<k || b<k) {
            out.println(-1);
        }
        else
            out.println(time);

        out.close();
    }


    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

}
