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

public class Solution {

    public void run() {
        int n = reader.nextInt();
        int k = reader.nextInt();
        ArrayList<Book> books = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            int t = reader.nextInt();
            int a = reader.nextInt();
            int b = reader.nextInt();
            books.add(new Book(t, a, b));
        }

        writer.println(find(n, k, books));
    }

    long find(int n, int k, ArrayList<Book> books) {
        Collections.sort(books, (o1, o2) -> {
            if (o1.t != o2.t) {
                return o1.t < o2.t ? -1 : 1;
            }
            int s1 = o1.a + o1.b;
            int s2 = o2.a + o2.b;
            if (s1 != s2) {
                return s1 > s2 ? -1 : 1;
            }
            return 0;
        });

        Stack<Book> a = new Stack<>();
        Stack<Book> b = new Stack<>();

        int alice = 0, bob = 0;
        long time = 0;

        for (Book book : books) {
            int s = book.a + book.b;
            if (s == 0) continue;

            if (alice < k && bob >= k && book.a == 0) continue;
            if (bob < k && alice >= k && book.b == 0) continue;

            alice += book.a;
            bob += book.b;
            time += book.t;

            if (s == 2) {
                if (alice > k && !a.isEmpty()) {
                    Book prev = a.pop();
                    alice -= prev.a;
                    time -= prev.t;
                }

                if (bob > k && !b.isEmpty()) {
                    Book prev = b.pop();
                    bob -= prev.b;
                    time -= prev.t;
                }
            } else {
                if (book.a > 0) {
                    a.add(book);
                }
                if (book.b > 0) {
                    b.add(book);
                }
            }

            if (alice >= k && bob >= k) {
                return time;
            }
        }

        return -1;
    }

    static class Book {
        int t, a, b;

        public Book(int t, int a, int b) {
            this.t = t;
            this.a = a;
            this.b = b;
        }
    }

    private InputReader reader;
    private PrintWriter writer;

    public Solution(InputReader reader, PrintWriter writer) {
        this.reader = reader;
        this.writer = writer;
    }

    public static void main(String[] args) {
        InputReader reader = new InputReader(System.in);
        PrintWriter writer = new PrintWriter(System.out);

        Solution solution = new Solution(reader, writer);
        solution.run();

        writer.flush();
    }

    static class InputReader {
        private static final int BUFFER_SIZE = 1 << 20;

        private BufferedReader reader;
        private StringTokenizer tokenizer;

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

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

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

        public long nextLong() {
            return Long.parseLong(nextToken());
        }
    }
}

