import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t;
        t = 1;



        while((t--) > 0 ){
            int n,m,k;


            n = in.nextInt();
            m = in.nextInt();
            k = in.nextInt();

            PriorityQueue<int[]> c = new PriorityQueue<>(new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] - o2[0];
                }
            });
            PriorityQueue<int[]> a = new PriorityQueue<>(new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] - o2[0];
                }
            });
            PriorityQueue<int[]> b = new PriorityQueue<>(new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] - o2[0];
                }
            });
            PriorityQueue<int[]> d = new PriorityQueue<>(new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] - o2[0];
                }
            });


            int time = 0;
            for(int i = 0; i < n ; i++){
                int ti = in.nextInt();
                int ai = in.nextInt();
                int bi = in.nextInt();

                if(ai==1 && bi==1){
                    c.add(new int[]{ti,i+1});
                } else if(ai==0 && bi==1){
                    b.add(new int[]{ti,i+1});
                } else if(ai==1 && bi==0){
                    a.add(new int[]{ti,i+1});
                } else {
                    d.add(new int[]{ti,i+1});
                }


            }

            int idx = 0;
            int ac = 0;
            int bc = 0;

            if(c.size() + Math.min(a.size(),b.size()) < k || (2*k - c.size()) > m){
                System.out.println(-1);
                continue;
            }

            int curr = k;

            List<Integer> ans = new ArrayList<>();

            int total = 0;
            while((2*curr) > m){

                int[] x = c.poll();

                time += x[0];
                ans.add(x[1]);

                curr -= 1;
                total += 1;
            }

            while(curr > 0){
                int[] aRes = new int[]{(int)1e6,-1};
                int[] bRes = new int[]{(int)1e6,-1};
                int[] cRes = new int[]{(int)1e6,-1};
                int amin = (int)(1e6);
                int bmin = (int)(1e6);
                int cmin = (int)(1e6);
                if(a.size() > 0){
                    aRes = a.peek();
                    amin = aRes[0];
                }
                if(b.size() > 0){
                    bRes = b.peek();
                    bmin = bRes[0];
                }
                if(c.size() > 0){
                    cRes = c.peek();
                    cmin = cRes[0];

                }
                if(amin + bmin < cmin){
                    time += amin + bmin;
                    ans.add(aRes[1]);
                    ans.add(bRes[1]);
                    a.poll();
                    b.poll();
                    total += 2;
                } else{
                    time += cmin;
                    ans.add(cRes[0]);
                    c.poll();
                    total += 1;
                }

                curr -= 1;
            }

            while(total < m){

                int[] aRes = new int[]{(int)1e6,-1};
                int[] bRes = new int[]{(int)1e6,-1};
                int[] cRes = new int[]{(int)1e6,-1};
                int[] dRes = new int[]{(int)1e6,-1};

                if(a.size() > 0){
                    aRes = a.peek();
//                     amin = aRes[0];
                }
                if(b.size() > 0){
                    bRes = b.peek();
//                    bmin = bRes[0];
                }
                if(c.size() > 0){
                    cRes = c.peek();
//                    cmin = cRes[0];

                }
                if(d.size() > 0){
                    dRes = d.peek();
                }

                if(aRes[0] <= bRes[0] && aRes[0] <= cRes[0] && aRes[0] <= dRes[0]){
                    time += aRes[0];
                    ans.add(aRes[1]);
                } else if(bRes[0] <= aRes[0] && bRes[0] <= cRes[0] && bRes[0] <= dRes[0]){
                    time += aRes[0];
                    ans.add(aRes[1]);
                } else if(cRes[0] <= aRes[0] && cRes[0] <= bRes[0] && cRes[0] <= dRes[0]){
                    time += cRes[0];
                    ans.add(cRes[1]);
                } else {
                    time += dRes[0];
                    ans.add(dRes[1]);
                }





                total += 1;

            }


            System.out.println(time);

            for(int q : ans){
                System.out.print(q + " ");
            }
            System.out.println();


        }

    }
}
