import java.util.*;
import java.io.IOException;
import java.io.InputStream;

public class Solution {
    public static void main(String[] args) throws IOException {
        int i, j;
        FastReader in = new FastReader(System.in);
        StringBuilder sb = new StringBuilder();
        Random rd=new Random();
        int t=1;
        while(t-->0) {
            int n = in.nextInt();
            int k = in.nextInt();
            /*int n=rd.nextInt(1000)+1;
            int k=rd.nextInt(n)+1;*/
            //System.out.println(n+" "+k);
            int arr[][] = new int[n][3];
            for (i = 0; i < n; i++) {
                for (j = 0; j < 3; j++)
                    if(j==0)
                        arr[i][j]=in.nextInt();
                    else
                        arr[i][j] = in.nextInt();
            }
            Integer ind[] = new Integer[n];
            for (i = 0; i < n; i++)
                ind[i] = i;

            Arrays.sort(ind, new Comparator<Integer>() {
                @Override
                public int compare(Integer a, Integer b) {
                    if (arr[a][0] != arr[b][0])
                        return Integer.compare(arr[a][0], arr[b][0]);
                    else {
                        return -Integer.compare(arr[a][1]+arr[a][2],arr[b][1]+arr[b][2]);
                    }
                }
            });
            int x = 0, y = 0, p1 = 0, p2 = 0;
            long cost = 0;
            int c1[] = new int[n];
            int c2[] = new int[n];
            for (i = 0; i < n; i++) {
                int idx = ind[i];
                if (arr[idx][1] == 1 || arr[idx][2] == 1) {
                    if (arr[idx][1] == 1 && arr[idx][2] == 1) {
                        x++;
                        y++;
                        cost += arr[idx][0];
                    } else if (arr[idx][1] == 1 && x < k) {
                        x++;
                        cost += arr[idx][0];
                        c1[p1++] = arr[idx][0];
                    } else if (arr[idx][2] == 1 && y < k) {
                        y++;
                        cost += arr[idx][0];
                        c2[p2++] = arr[idx][0];
                    }
                }
                if (x >= k && y >= k)
                    break;
            }
            //System.out.println(x + " " + y);
            if (x < k || y < k) {
                sb.append("-1");
            } else {
                if (x > k) {
                    int z = x - k;
                    while (z != 0 && p1 > 0) {
                        cost -= c1[--p1];
                        z--;
                    }
                }
                if (y > k) {
                    int z = y - k;
                    while (z != 0 && p2 > 0) {
                        cost -= c2[--p2];
                        z--;
                    }
                }
                sb.append(cost);
            }
        }
        System.out.println(sb);
    }
}

class FastReader {

    byte[] buf = new byte[2048];
    int index, total;
    InputStream in;

    FastReader(InputStream is) {
        in = is;
    }

    int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) {
                return -1;
            }
        }
        return buf[index++];
    }

    String next() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }

    int nextInt() throws IOException {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }

    long nextLong() throws IOException {
        int c;
        long val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
}


