import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class CF1374E1 {


    public static void main(String[] args) {
        FastReader input = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);

        int n = input.nextInt();
        int k = input.nextInt();

        ArrayList<Long> alice = new ArrayList<>();
        ArrayList<Long> bob = new ArrayList<>();

        ArrayList<Long> aliceAndBob = new ArrayList<>();

        for(int i = 0;i < n;i++){
            long time = input.nextLong();
            int ai = input.nextInt();
            int bi = input.nextInt();
            if(ai == 1 && bi == 1){
                aliceAndBob.add(time);
            }
            else{
                if(ai == 1){
                    alice.add(time);
                }
                else{
                    bob.add(time);
                }
            }
        }

        Collections.sort(aliceAndBob);
        Collections.sort(alice);
        Collections.sort(bob);

        boolean possible = true;

        int a = 0;
        int b = 0;
        int ab = 0;

        int done = 0;
        long totalTime = 0;


        while (done < k){
            if(a >= alice.size() || b >= bob.size()){
                if(ab >= aliceAndBob.size()){
                    possible = false;
                    break;
                }
                else{
                    totalTime += aliceAndBob.get(ab);
                    ab++;
                    done++;
                }
            }
            else if(ab >= aliceAndBob.size()){
                if(a >= alice.size() || b >= bob.size()){
                    possible = false;
                    break;
                }
                else{
                    totalTime += alice.get(a) + bob.get(b);
                    a++;
                    b++;
                    done++;
                }
            }


            else {
                if((alice.get(a) + bob.get(b) <= aliceAndBob.get(ab))){
                    totalTime += alice.get(a) + bob.get(b);
                    a++;
                    b++;
                    done++;
                }
                else{
                    totalTime += aliceAndBob.get(ab);
                    ab++;
                    done++;
                }
            }
        }

        if(possible == false){
            pw.println(-1);
        }
        else{
            pw.println(totalTime);
        }
        pw.flush();
        pw.close();
    }
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}
