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

public class E1 {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        Reader s = new Reader();
        int n = s.i() , x = s.i();
        ArrayList<Integer> both = new ArrayList<>();
        ArrayList<Integer> alice = new ArrayList<>();
        ArrayList<Integer> bob = new ArrayList<>();
        for (int i=0;i<n;i++) {
            int t = s.i() , a= s.i() , b = s.i();
            if (a==1 && b == 1) both.add(t);
            else if (a == 1) alice.add(t);
            else if (b == 1) bob.add(t);
        }
        Collections.sort(bob);
        Collections.sort(alice);
        Collections.sort(both);
        int i = 0 , j = 0 , k = 0;
        int count1 = 0 , count2 = 0;
        int ans = 0;
        while (i < both.size() && j < bob.size() && k < alice.size()) {
            if (count1 == x && count2 == x) break;
            int a = both.get(i);
            int b = bob.get(j);
            int c = alice.get(k);
            if (b+c < a) {
                ans += b+c;
                count1++;
                count2++;
                j++;
                k++;
            } else {
                ans += a;
                count1++;
                count2++;
                i++;
            }
        }
        if (count1 == x && count2 == x) {
            out.println(ans);
            out.flush();
            return;
        }
        if (i == both.size()) {
            while (j < bob.size() && k < alice.size()) {
                if (count1 == x && count2 == x) break;
                ans += bob.get(j);
                ans += alice.get(k);
                j++;
                k++;
                count1++;
                count2++;
            }
            if (count1 != x || count2 != x) {
                out.println(-1);
                out.flush();
                return;
            }
            out.println(ans);
        } else {
            while (i < both.size()) {
                if (count1 == x && count2 == x) break;
                ans += both.get(i);
                i++;
                count1++;
                count2++;
            }
            if (count1 != x || count2 != x) {
                out.println(-1);
                out.flush();
                return;
            }
            out.println(ans);
        }
        out.flush();
    }

    private static int power(int a, int n) {
        int result = 1;
        while (n > 0) {
            if (n % 2 == 0) {
                a = (a * a);
                n /= 2;
            } else {
                result = (result * a);
                n--;
            }
        }
        return result;
    }

    private static int power(int a, int n, int p) {
        int result = 1;
        while (n > 0) {
            if (n % 2 == 0) {
                a = (a * a) % p;
                n /= 2;
            } else {
                result = (result * a) % p;
                n--;
            }
        }
        return result;
    }

    static class Reader {
        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar, numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String s() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long l() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int i() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double d() throws IOException {
            return Double.parseDouble(s());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int[] arr(int n) {
            int[] ret = new int[n];
            for (int i = 0; i < n; i++) {
                ret[i] = i();
            }
            return ret;
        }

        public long[] arrLong(int n) {
            long[] ret = new long[n];
            for (int i = 0; i < n; i++) {
                ret[i] = l();
            }
            return ret;
        }
    }
    //    static class pairLong implements Comparator<pairLong> {
//        long first, second;
//
//        pairLong() {
//        }
//
//        pairLong(long first, long second) {
//            this.first = first;
//            this.second = second;
//        }
//
//        @Override
//        public int compare(pairLong p1, pairLong p2) {
//            if (p1.first == p2.first) {
//                if(p1.second > p2.second) return 1;
//                else return -1;
//            }
//            if(p1.first > p2.first) return 1;
//            else return -1;
//        }
//    }
//    static class pair implements Comparator<pair> {
//        int first, second;
//
//        pair() {
//        }
//
//        pair(int first, int second) {
//            this.first = first;
//            this.second = second;
//        }
//
//        @Override
//        public int compare(pair p1, pair p2) {
//            if (p1.first == p2.first) return p1.second - p2.second;
//            return p1.first - p2.first;
//        }
//    }
}
