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.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;
            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();
                if (tm2.containsKey(t)) {
                    int val2 = tm2.get(t);
                    int min = Math.min(val, val2);
                    ans += 1l * min * t.a;
                    if (val - min > 0) tm1.put(t, val - min);
                    else tm1.remove(t);
                    if (val2 - min > 0) tm2.put(t, val2 - min);
                    else tm2.remove(t);
                    c1 = Math.max(0, c1 - min);
                    c2 = Math.max(0, c2 - min);
                }
            }
//        pw.println(tm1);
//        pw.println(tm2);
//        pw.println(c1+" "+c2+" "+ans);
            while (c1 > 0 && tm1.size() > 0) {
                pair t = tm1.firstKey();
                int val = tm1.firstEntry().getValue();
                int min = Math.min(c1, val);
                int x = tm1.pollFirstEntry().getKey().a;
                ans += 1l * min * x;
                c1 -= min;

            }
            while (c2 > 0 && tm2.size() > 0) {
                pair t = tm2.firstKey();
                int val = tm2.firstEntry().getValue();
                int min = Math.min(c2, val);
                int x = tm2.pollFirstEntry().getKey().a;
                ans += 1l * min * x;
                c2 -= min;
            }
//        pw.println(tm1);
//        pw.println(tm2);
//        pw.println(c1+" "+c2+" "+ans);
            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 ? c - pair.c : b - pair.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());
        }

    }
}

