import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1ReadingBooksEasyVersion {
        public void solve(int testNumber, Scanner sc, PrintWriter pw) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            TreeMap<pair, Integer> tm1 = new TreeMap<>();
            TreeMap<pair, Integer> tm2 = new TreeMap<>();
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt();
                int t1 = sc.nextInt();
                int t2 = sc.nextInt();
                if (t1 == 1) tm1.put(new pair(x, t1, t2), tm1.getOrDefault(new pair(x, t1, t2), 0) + 1);
                if (t2 == 1) tm2.put(new pair(x, t1, t2), tm2.getOrDefault(new pair(x, t1, t2), 0) + 1);
            }

            int c1 = k;
            int c2 = k;
            long ans = 0;
            TreeSet<pair1> ts1 = new TreeSet<>();
            TreeSet<pair1> ts2 = new TreeSet<>();
            TreeMap<pair, Integer> tmp = new TreeMap<>(tm1);
//        pw.println(tm1);
//        pw.println(tm2);
            while (tmp.size() > 0) {
                pair t = tmp.firstKey();
                int val = tmp.pollFirstEntry().getValue();
                int a = t.a;
                int b = t.b;
                int c = t.c;
                for (int i = 1; i <= val; i++) {
                    ts1.add(new pair1(a, c, b, i));
                }

            }
            TreeMap<pair, Integer> tmp1 = new TreeMap<>(tm2);
            while (tmp1.size() > 0) {
                pair t = tmp1.firstKey();
                int val = tmp1.pollFirstEntry().getValue();
                int a = t.a;
                int b = t.b;
                int c = t.c;
                for (int i = 1; i <= val; i++) {
                    ts2.add(new pair1(a, b, c, i));
                }

            }
            while (c1 > 0 && ts1.size() > 0) {
                pair1 t = ts1.pollFirst();
                if (t.c == 1) c1--;
                if (t.b == 1) c2--;
                ans += 1l * t.a;
                ts2.remove(t);
            }
            while (c2 > 0 && ts2.size() > 0) {
                pair1 t = ts2.pollFirst();
                if (t.b == 1) c1--;
                if (t.c == 1) c2--;
                ans += 1l * t.a;
                ts1.remove(t);
            }

            pw.println((c1 <= 0 && c2 <= 0) ? ans : -1);
        }

        public class pair implements Comparable<pair> {
            int a;
            int b;
            int c;

            public pair(int a, int b, int c) {
                this.a = a;
                this.b = b;
                this.c = c;
            }

            public int compareTo(pair pair) {
                return a - pair.a == 0 ? b - pair.b == 0 ? pair.c - c : pair.b - c : a - pair.a;
            }

            public String toString() {
                return a + " " + b + " " + c;
            }

        }

        public class pair1 implements Comparable<pair1> {
            int a;
            int b;
            int c;
            int d;

            public pair1(int a, int b, int c, int d) {
                this.a = a;
                this.b = b;
                this.c = c;
                this.d = d;
            }

            public int compareTo(pair1 pair) {
                return a - pair.a == 0 ? b - pair.b == 0 ? c - pair.c == 0 ? d - pair.d : pair.c - c : pair.b - b : a - pair.a;
            }

            public String toString() {
                return a + " " + b + " " + c;
            }

        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

    }
}

