import java.io.*;
import java.util.*;
//import java.util.HashMap;


//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.io.PrintWriter;
//import java.math.BigInteger;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.StringTokenizer;

public class Equal {
    static ArrayList<Integer>[] tree;
    static boolean[] vis;
    static int dp[][];

    //    static boolean dfs(int u, int dest) {
    //        if (dp[u][dest] != 0) {
    //            if (dp[u][dest] == 1)
    //                return true;
    //            return false;
    //        }
    //        vis[u] = true;
    //        if (u == dest) {
    //            vis[u] = false;
    //            dp[u][dest] = 1;
    //            return true;
    //        }
    //        for (int i = 0; i < tree[u].size(); i++) {
    //            int v = tree[u].get(i);
    //            if (vis[v])
    //                continue;
    //            if (dfs(v, dest)) {
    //                vis[u] = false;
    //                dp[u][dest] = 1;
    //                dp[dest][u] = 1;
    //
    //                return true;
    //            }
    //
    //        }
    //        vis[u] = false;
    //        dp[u][dest] = -1;
    //        dp[dest][u] = -1;
    //        return false;
    //    }
    static class pair implements Comparable<pair> {
        int x;
        int t;

        public pair(int xx, int tt) {
            x = xx;
            t = tt;
        }

        @Override
        public int compareTo(pair o) {
            return t - o.t;
        }
    }

    static boolean dif1(String x, String y) {
        int dif = 0;
        if (x.length() != y.length())
            return false;
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) != y.charAt(i))
                dif++;
        }
        return dif <= 1;
    }

    static int[] p;
    static int[] rank;

    static int findSet(int i) {
        return (p[i] == i) ? i : (p[i] = findSet(p[i]));
    }

    static boolean isSameSet(int i, int j) {
        return findSet(i) == findSet(j);
    }

    static void unionSet(int i, int j) {
        if (!isSameSet(i, j)) {
            int x = findSet(i);
            int y = findSet(j);
            if (rank[x] > rank[y]) {
                p[y] = x;
            } else {
                p[x] = y;
                if (rank[x] == rank[y])
                    rank[y]++;
            }
        }
    }


    public static void main(String[] args) throws IOException {
        //BufferedReader br = new BufferedReader(new FileReader("name.in"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        PrintWriter out = new PrintWriter(System.out);

        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        PriorityQueue<pair> notL = new PriorityQueue<>();
        PriorityQueue<pair> AB = new PriorityQueue<>();
        PriorityQueue<pair> B = new PriorityQueue<>();
        PriorityQueue<pair> A = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            pair t = new pair(i, Integer.parseInt(st.nextToken()));
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            if (a == 1 && b == 1)
                AB.add(t);
            if (a == 0 && b == 0)
                notL.add(t);
            if (a == 0 && b == 1)
                B.add(t);
            if (a == 1 && b == 0)
                A.add(t);

        }
        long t = 0;
        ArrayList<pair> M = new ArrayList<>();
        while (k > 0 && m > 0) {
            if (A.isEmpty() && B.isEmpty() && AB.isEmpty())
                break;
            if (A.isEmpty() || B.isEmpty()) {
                if (AB.isEmpty())
                    break;
                t += AB.peek().t;
                M.add(AB.poll());
                k--;
                m--;

            } else {
                if (AB.isEmpty()) {

                    if (m >= 2) {
                        t += A.peek().t + B.peek().t;
                        k--;
                        m -= 2;
                        M.add(A.poll());
                        M.add(B.poll());
                    } else break;
                } else {
                    if (A.peek().t + B.peek().t < AB.peek().t && m >= 2) {
                        t += A.peek().t;
                        t += B.peek().t;
                        k--;
                        m -= 2;
                        M.add(A.poll());
                        M.add(B.poll());
                    } else {
                        t += AB.peek().t;
                        k--;
                        m--;
                        M.add(AB.poll());
                    }

                }
            }


        }
        if (k != 0)
            out.println(-1);
        else {
            while (!A.isEmpty())
                notL.add(A.poll());
            while (!B.isEmpty())
                notL.add(B.poll());
            while (!AB.isEmpty())
                notL.add(AB.poll());
            while (m > 0) {
                if (notL.isEmpty())
                    break;
                t += notL.peek().t;
                M.add(notL.poll());
                m--;
            }
            if (m != 0)
                out.println(-1);
            else {
                out.println(t);
                for (int i = 0; i <M.size() ; i++) {
                    out.print(M.get(i).x+1);
                    if(i<M.size()-1)
                        out.print(" ");
                }
            }
        }


        out.flush();
        out.close();

    }


}
