import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @project: Codeforces653
 * @author: izhanatuly
 * @date: 28/06/2020
 */
public class E1 {
    static Scanner scanner;

    public static void main(String[] args) {
        scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        runTestCase();
    }

    public static void runTestCase() {
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        List<Book> books = new ArrayList<>();
        int canA = 0;
        int canB = 0;
        for (int i = 0; i < n; i++) {
            int t = scanner.nextInt();
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if(a == 1) {
                canA++;
            }
            if(b == 1) {
                canB++;
            }
            if(a==1 || b==1) books.add(new Book(t, a ,b));
        }
        if(canA < k || canB < k) {
            System.out.println(-1);
            return;
        }
        long sum = 0;
        PriorityQueue<Book> both = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
        PriorityQueue<Book> onlyA = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
        PriorityQueue<Book> onlyB = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
        for(Book book : books) {
            sum += book.time;
            if(book.a == 1 && book.b == 1) {
                both.add(book);
            } else if(book.a == 1) {
                onlyA.add(book);
            } else {
                onlyB.add(book);
            }
        }
        while(canA >= k && canB >= k) {
            Book fromOnlyA = onlyA.peek();
            Book fromOnlyB = onlyB.peek();
            Book fromBoth = both.peek();
            if(canA > k && canB > k) {
                long justA = (fromOnlyA != null ? fromOnlyA.time : 0);
                long justB = (fromOnlyB != null ? fromOnlyB.time : 0);
                long ab = justA + justB;
                long bothTime = (fromBoth != null ? fromBoth.time : 0);
                if(justA >= bothTime && !onlyA.isEmpty()) {
                    canA--;
                    Book book = onlyA.poll();
                    sum -= book.time;
                } else if(justB >= bothTime && !onlyB.isEmpty()){
                    canB--;
                    Book book = onlyB.poll();
                    sum -= book.time;
                } else if(ab > bothTime) {
                    if(!onlyA.isEmpty()) {
                        Book book1 = onlyA.poll();
                        sum -= book1.time;
                        canA--;
                    }
                    if(!onlyB.isEmpty()) {
                        Book book2 = onlyB.poll();
                        sum -= book2.time;
                        canB--;
                    }
                } else {
                    canA--;
                    canB--;
                    Book book = both.poll();
                    sum -= book.time;
                }
            } else if(canA > k) {
                canA--;
                Book book = onlyA.poll();
                sum -= book.time;
            } else if(canB > k) {
                canB--;
                Book book = onlyB.poll();
                sum -= book.time;
            } else if(canA == k && canB == k) {
                break;
            }
        }
        System.out.println(sum);
    }
}

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