import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class e653_1 {
    static PrintWriter out;
    static BufferedReader in;
    static StringTokenizer st;

    public static void main(String[] args) throws FileNotFoundException {
        out = new PrintWriter(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
        new e653_1().Run();
        out.close();
    }

    String ns() {
        try {
            if (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        } catch (Exception e) {
            return null;
        }
    }

    int nextint() {
        return Integer.valueOf(ns());
    }

    private int n;
    private int k;

    int inf = (int) Math.pow(2,32)/2 - 1;
    LinkedList<Integer> acceptableA = new LinkedList<>();
    LinkedList<Integer> acceptableB = new LinkedList<>();
    LinkedList<Integer> acceptable = new LinkedList<>();

    public void Run() {
        n = nextint();
        k = nextint();

        int aPos = 0;
        int bPos = 0;
        for(int i = 0; i < n; i++){
            int t = nextint();
            int a = nextint();
            int b = nextint();

            if(a == 1 && b == 1){
                acceptable.add(t);
                aPos++;
                bPos++;
            }
            else if (a == 1){
                aPos++;
                acceptableA.add(t);
            }
            else if (b == 1){
                bPos++;
                acceptableB.add(t);
            }
        }
        if (aPos < k || bPos < k){
            out.println(-1);
            return;
        }
        Collections.sort(acceptableB);
        Collections.sort(acceptableA);
        Collections.sort(acceptable);

        int aExpected = k;
        int bExpected = k;
        long sum = 0;

        while(aExpected > 0 || bExpected > 0){

            int a = inf;
            int b = inf;
            int c = inf;

            if(acceptableA.size() > 0) a = acceptableA.peek();
            if(acceptableB.size() > 0) b = acceptableB.peek();
            if(acceptable.size() > 0) c = acceptable.peek();

            if (aExpected > 0 && aExpected >= bExpected){
                if (a < c) {
                    aExpected--;
                    acceptableA.remove();
                    sum+=a;
                }
                else if (c!=inf){
                    aExpected--;
                    bExpected--;
                    acceptable.remove();
                    sum+=c;
                }
            }
            else if (bExpected > 0){
                if (b < c){
                    bExpected--;
                    acceptableB.remove();
                    sum+=b;
                }
                else if(c!=inf){
                    aExpected--;
                    bExpected--;
                    acceptable.remove();
                    sum+=c;
                }
            }
        }
        out.println(sum);
    }
}