
import java.io.*;
import java.util.*;

public class E {
    Random random = new Random(751454315315L + System.currentTimeMillis());

    public void solve() throws IOException {
        int n = nextInt(), k = nextInt();
        List<Integer> both = new ArrayList<>();
        List<Integer> alice = new ArrayList<>();
        List<Integer> bob = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int t = nextInt(), a = nextInt(), b = nextInt();
            if (a + b == 2) {
                both.add(t);
            } else if (a == 1) {
                alice.add(t);
            } else {
                bob.add(t);
            }
        }
        if (both.size() + alice.size() < k || both.size() + bob.size() < k) {
            out.println(-1);
            return;
        }
        Collections.sort(bob);
        Collections.sort(both);
        Collections.sort(alice);
        int i = 0, j = 0, read = 0;
        long ans = 0;
        while (read < k) {
            if (i < bob.size() && i < alice.size() && j < both.size() && bob.get(i) + alice.get(i) < both.get(j) || j >= both.size()) {
                ans += bob.get(i) + alice.get(i);
                i++;
            } else {
                ans += both.get(j);
                j++;
            }
            read++;
        }
        out.println(ans);
    }

    public void run() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private void shuffle(int[] s) {
        for (int i = 0; i < s.length; ++i) {
            int j = random.nextInt(i + 1);
            int t = s[i];
            s[i] = s[j];
            s[j] = t;
        }
    }

    static class Pair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<Pair<T1, T2>> {
        T1 f;
        T2 s;

        public Pair(T1 f, T2 s) {
            this.f = f;
            this.s = s;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Pair<T1, T2> pair = (Pair<T1, T2>) o;
            return f == pair.f &&
                    s == pair.s;
        }

        @Override
        public int hashCode() {
            return Objects.hash(f, s);
        }

        @Override
        public int compareTo(Pair<T1, T2> o) {
            if (f.compareTo(o.f) == 0) {
                return s.compareTo(o.s);
            }
            return f.compareTo(o.f);
        }
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    public String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public int[] nextArr(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = nextInt();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new E().run();
    }
}