
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class ReadingBooks1 {

	static class Book {
		int time;
		int alice;
		int bob;

		public Book(int time, int alice, int bob) {
			super();
			this.time = time;
			this.alice = alice;
			this.bob = bob;
		}

	}

	static int k;

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int noBooks = scan.nextInt();
		k = scan.nextInt();
		Book[] list = new Book[noBooks];
		for (int i = 1; i <= noBooks; i++) {
			list[i - 1] = new Book(scan.nextInt(), scan.nextInt(), scan.nextInt());
		}
		Arrays.sort(list, new Comparator<Book>() {

			@Override
			public int compare(Book b1, Book b2) {
				int diff =  (b2.alice + b2.bob)- (b1.alice + b1.bob);
				if (diff != 0)
					return diff;
				else
					return b1.time - b2.time;
			}
		});

		long sum = new ReadingBooks1().getMinSum(list, 0, 0, 0);
		if(sum<Integer.MAX_VALUE)
		System.out.println(sum);
		else
			System.out.println(-1);
	}

	public long getMinSum(Book[] list, int a1, int b1, int index) {
		if (index >= list.length - 1 && (a1 != k || b1 != k))
			return Integer.MAX_VALUE;
		if (a1 == k && b1 == k)
			return 0;
		Book t = list[index];
		long sum1 = t.time + getMinSum(list, a1 + t.alice, b1 + t.bob, index + 1);
		//long sum2 = getMinSum(list, a1, b1, index + 1);
		//return Math.min(sum1, sum2);
		return sum1;
	}
}
