import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.util.Collections;
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;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1ReadingBooksEasyVersion {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            ArrayList<E1ReadingBooksEasyVersion.Pair> list = new ArrayList<>();
            int ca = 0, cb = 0;
            for (int i = 0; i < n; i++) {
                int t = in.nextInt();
                int a = in.nextInt();
                int b = in.nextInt();
                if (a == 0 && b == 0) continue;
                list.add(new E1ReadingBooksEasyVersion.Pair(t, a, b));
                ca = Math.min(k, (a == 1 ? 1 : 0) + ca);
                cb = Math.min(k, (b == 1 ? 1 : 0) + cb);
            }
            if (ca < k || cb < k) {
                out.println(-1);
                return;
            }
            Collections.sort(list);
            int total = 0;
            int i = 0;
            boolean[] taken = new boolean[list.size()];
            while (ca > 0 || cb > 0) {
                E1ReadingBooksEasyVersion.Pair p = list.get(i);
                if ((ca <= 0 && p.b == 0) || (cb <= 0 && p.a == 0)) {
                    i++;
                    continue;
                }
                taken[i] = true;
                total += p.time;
                ca -= p.a;
                cb -= p.b;
                i++;
            }
            i = list.size() - 1;
            while (ca < 0 && i >= 0) {
                E1ReadingBooksEasyVersion.Pair p = list.get(i);
                if (p.a == 1 && p.b == 0 && taken[i]) {
                    total -= p.time;
                    ca++;
                    taken[i] = false;
                }
                i--;
            }
            i = list.size() - 1;
            while (cb < 0 && i >= 0) {
                E1ReadingBooksEasyVersion.Pair p = list.get(i);
                if (p.b == 1 && p.a == 0 && taken[i]) {
                    total -= p.time;
                    cb++;
                    taken[i] = false;
                }
                i--;
            }
            out.println(total);
        }

        static class Pair implements Comparable<E1ReadingBooksEasyVersion.Pair> {
            int time;
            int a;
            int b;

            Pair(int time, int a, int b) {
                this.time = time;
                this.a = a;
                this.b = b;
            }

            public int compareTo(E1ReadingBooksEasyVersion.Pair v) {
                if (time == v.time) return Math.abs(a - b) - Math.abs(v.a - v.b);
                return Integer.compare(time, v.time);
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

    }
}

