import java.util.*;

public class Solution {


    public static void mainB(String[] args) {
        final Scanner in = new Scanner(System.in);

        final int T = in.nextInt();

        for (int t = 0; t < T; t += 1) {
            int n = in.nextInt();


            int count2 = 0;

            while (n % 2 == 0) {
                n /= 2;
                count2 += 1;
            }

            int count3 = 0;
            while (n % 3 == 0) {
                n /= 3;
                count3 += 1;
            }

            System.out.println((n > 1 || count2 > count3)? -1 : (count2 + 2 * (count3 - count2)));
        }
    }

    public static void mainC(String[] args) {
        final Scanner in = new Scanner(System.in);

        final int T = in.nextInt();

        for (int t = 0; t < T; t += 1) {
            int n = in.nextInt();
            in.nextLine();

            char[] s = in.nextLine().trim().toCharArray();

            int b = 0;
            int cost = 0;

            for (char c : s) {
                if (c == ')') b -= 1;
                else b += 1;

                if (b < 0) {
                    b = 0;
                    cost += 1;
                }
            }


            System.out.println(cost);
        }
    }

    public static void mainD(String[] args) {
        final Scanner in = new Scanner(System.in);

        final int T = in.nextInt();

        for (int t = 0; t < T; t += 1) {
            final int n = in.nextInt();
            final long k = in.nextLong();


            final long[] a = new long[n];
            for (int i = 0; i < n; i += 1) a[i] = in.nextLong();

            final Map<Long, Long> r = new HashMap<>();

            for (long ai : a) r.put(ai % k, r.getOrDefault(ai % k, 0L) + 1);

            if (r.getOrDefault(0L, 0L) == n) {
                System.out.println(0);
                continue;
            }

            long max = 0;
            long minR = 0;

            for (Map.Entry<Long, Long> e : r.entrySet()) {
                if (e.getKey() == 0) continue;
                if (e.getValue() > max || e.getValue() == max && e.getKey() < minR) {
                    max = e.getValue();
                    minR = e.getKey();
                }
                max = Math.max(max, e.getValue());
            }

            final long moves = (max - 1) * k + k - minR + 1;


            System.out.println(moves);
        }
    }

    public static void mainE1(String[] args) {
        final Scanner in = new Scanner(System.in);

        final int n = in.nextInt();
        final int k = in.nextInt();


        final int[][] tab = new int[n][];
        for (int i = 0; i < n; i += 1) tab[i] = new int[] {in.nextInt(), in.nextInt(), in.nextInt()};

        final Comparator<int[]> CMP = new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                final int likeA = a[1] + a[2];
                final int likeB = b[1] + b[2];

                int cmp = Integer.compare(likeB, likeA);

                if (cmp != 0) return cmp;

                cmp = Integer.compare(b[1], a[1]);

                if (cmp != 0) return cmp;

                return Integer.compare(a[0], b[0]);
            }
        };

        Arrays.sort(tab, CMP);

        // 0 == both, 1 = A, 2 = B, 3 = neither
        int state = 0;

        int i = 0;
        int likeA = 0;
        int likeB = 0;
        long total = 0;

        while (i < n && (likeA < k || likeB < k)) {
            int[] b = tab[i];

            if (b[1] + b[2] == 2) state = 0;
            else if (b[1] == 1) state = 1;
            else if (b[2] == 1) state = 2;
            else state = 3;

            if (state == 1 && likeA >= k) {
                // skip to books that B likes
                // binary search;

                int lo = i + 1;
                int hi = n;

                i = n;

                while (lo < hi) {
                    int mid = lo + (hi - lo) / 2;

                    if (tab[mid][2] == 1) {
                        i = Math.min(i, mid);
                        hi = mid;
                    } else lo = mid + 1;
                }
            } else if ((state == 2 && likeA < k) || state == 3) {
                System.out.println(-1);
                return;
            }

            likeA += tab[i][1];
            likeB += tab[i][2];
            total += (long)tab[i][0];
            i += 1;
        }

        System.out.println(likeA >= k && likeB >= k ? total : -1);
    }

    public static void main(String[] args) {
        mainE1(args);
    }
}
