

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;

public final class E {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int n = fs.nextInt();
        final int k = fs.nextInt();
        List<int[]> books = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            final int[] book = { fs.nextInt(), fs.nextInt(), fs.nextInt() };
            if (book[1] == 0 && book[2] == 0) {
                continue;
            }
            books.add(book);
        }
        books.sort((a, b) -> a[1] == b[1] ? Integer.compare(a[0], b[0]) : Integer.compare(b[1], a[1]));
        long res = (long) 1e18;
        long currSum = 0;
        int j = 0;
        int windowA = k;
        int windowB = k;
        for (int i = 0; i < books.size(); i++) {
            final int take = Math.max(books.get(i)[1], books.get(i)[2]);
            windowA -= books.get(i)[1];
            windowB -= books.get(i)[2];
            if (take > 0) {
                currSum += books.get(i)[0];
            }
            while (Math.max(windowA, windowB) < 0) {
                final int remove = Math.max(books.get(j)[1], books.get(j)[2]);
                windowA += books.get(j)[1];
                windowB += books.get(j)[2];
                if (remove > 0) {
                    currSum -= books.get(j)[0];
                }
                j++;
            }
            if (Math.max(windowA, windowB) == 0) {
                res = Math.min(res, currSum);
            }
        }
        System.out.println(res == (long) 1e18 ? -1 : res);
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}
