import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static class Pair {
        int t;
        int love;

        Pair (int t, int love) {
            this.t = t;
            this.love = love;
        }
    }

    public static void main(String[] args) {
        FastReader scan = new FastReader();

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

        int cntAlice = 0;
        int cntBob = 0;

        Pair[] arr = new Pair[n];
        for(int i=0;i<n;i++) {
            int t = scan.nextInt();
            int a = scan.nextInt();
            int b = 2*scan.nextInt();
            arr[i] = new Pair(t, a+b);
            if(a != 0) {
                cntAlice++;
            }
            if(b != 0) {
                cntBob++;
            }
        }

        if(cntAlice < k || cntBob < k) {
            System.out.println(-1);
            return;
        }

        Arrays.sort(arr,((o1, o2) -> {
            if(o1.t == o2.t) {
                return o2.love - o1.love;
            }
            return o1.t-o2.t;
        }));

/*
        for(Pair p : arr) {
            System.out.println(p.t+" "+(p.love > 2 ? "1 1" : (p.love > 1 ? "0 1" : (p.love > 0 ? "1 0" :"0 0"))));
        }*/

        Stack<Integer> alice = new Stack<>();
        Stack<Integer> bob = new Stack<>();
        long ans = 0;
        int cnt = 0;
        for(Pair p : arr) {

            if(p.love == 0) {
                continue;
            }

            if(p.love == 3) {
                //alice & bob
                cnt++;
                if(!alice.isEmpty() && cnt+alice.size() > k) {
                    ans -= alice.pop();
                }
                if(!bob.isEmpty()  &&  cnt+bob.size() > k) {
                    ans -= bob.pop();
                }

                ans += p.t;
            } else if(p.love == 1) {
                //alice only
                if(alice.size()+cnt < k) {
                    ans += p.t;
                    alice.add(p.t);
                }
            } else if(p.love == 2) {
                //bob only
                if(bob.size()+cnt < k) {
                    ans += p.t;
                    bob.add(p.t);
                }
            }

            //System.out.println(ans);
            if(alice.size()+cnt >= k && bob.size()+cnt >= k) {
                break;
            }
        }

        System.out.println(ans);
    }








    //fast input reader
    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;
        }
    }
}


