import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;

public class Template {
    static class Task {
        int n, k;
        int[] t, a, b;

        public void solve(InputReader in, PrintWriter out) {
            n = in.nextInt();
            k = in.nextInt();
            t = new int[n];
            a = new int[n];
            b = new int[n];

            for (int i = 0; i < n; i++) {
                t[i] = in.nextInt();
                a[i] = in.nextInt();
                b[i] = in.nextInt();
            }

            ArrayList<Integer> alice = new ArrayList<>(), bob = new ArrayList<>(), both = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (a[i] == 1 && b[i] == 1) {
                    both.add(t[i]);
                } else if (a[i] == 1) {
                    alice.add(t[i]);
                } else {
                    bob.add(t[i]);
                }
            }

            Collections.sort(alice);
            Collections.sort(bob);

            for (int i = 0; i < min(alice.size(), bob.size()); i++) {
                both.add(alice.get(i) + bob.get(i));
            }

            Collections.sort(both);
            if (both.size() < k) {
                out.println(-1);
            } else {
                int total = 0;
                for (int i = 0; i < k; i++) {
                    total += both.get(i);
                }
                out.println(total);
            }
        }
    }


    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        int t = 1;
        // t = in.nextInt();
        for (int i = 1; i <= t; i++) {
            // out.print("Case #" + i + ": ");
            Task solver = new Task();
            solver.solve(in, out);
        }
        out.close();
    }


    /**
     * gcd/lcm
     */
    static int gcd(int a, int b) {
        if (a == 0) {
            return b;
        } else {
            return gcd(b % a, a);
        }
    }

    static long gcd(long a, long b) {
        if (a == 0) {
            return b;
        } else {
            return gcd(b % a, a);
        }
    }

    static long lcm(int a, int b) {
        return (long) a / gcd(a, b) * b;
    }

    static long lcm(long a, long b) {
        return a / gcd(a, b) * b;
    }

    /*
     * Modular arithmetic
     */
    static int modPow(int base, int exponent, int mod) {
        if (exponent == 0) {
            return 1 % mod;
        }
        long ret = modPow(base, exponent / 2, mod);
        ret = (ret * ret) % mod;
        if (exponent % 2 == 1) {
            ret = (ret * base) % mod;
        }
        return (int) ret;
    }

    static int inv(int number, int prime) {
        return modPow(number, prime - 2, prime);
    }

    static final int MOD = (int) 1e9 + 7;

    static int add(int a, int b) {
        return (a + b) % MOD;
    }

    static int sub(int a, int b) {
        int ret = (a - b) % MOD;
        return (ret < 0) ? ret + MOD : ret;
    }

    static int mul(int a, int b) {
        return (int) (((long) a * b) % MOD);
    }

    static int divide(int a, int b) {
        return mul(a, inv(b, MOD));
    }


    /**
     * IO
     */
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

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

        public int[] nextIntArray(int n) {
            int[] ret = new int[n];
            Arrays.setAll(ret, i -> nextInt());
            return ret;
        }

        public double[] nextDoubleArray(int n) {
            double[] ret = new double[n];
            Arrays.setAll(ret, i -> nextDouble());
            return ret;
        }

        public long[] nextLongArray(int n) {
            long[] ret = new long[n];
            Arrays.setAll(ret, i -> nextLong());
            return ret;
        }
    }
}
