import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.StreamSupport;

/**
 * Created by Harry on 5/31/20.
 */
public class Test {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();
        int[][] books = new int[n][3];
        HashSet<Integer> used = new HashSet<>();
        PriorityQueue<Integer> cQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0]));
        PriorityQueue<Integer> aQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0]));
        PriorityQueue<Integer> bQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0]));
        for(int i=0; i<n; i++){
            int t = scanner.nextInt();
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            books[i][0] = t;
            books[i][1] = a;
            books[i][2] = b;
            if(a+b==2){
                cQ.offer(i);
            }
            else if(a==1){
                aQ.offer(i);
            }
            else if(b==1){
                bQ.offer(i);
            }
        }
        PriorityQueue<Integer> cCostQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[b][0],books[a][0]));
        Queue<Integer> aCostQ = new LinkedList<>();
        Queue<Integer> bCostQ = new LinkedList<>();
        int ak = 0;
        int bk = 0;
        int cm = 0;
        while(ak<k && bk<k && !cQ.isEmpty()){
            int cur = cQ.poll();
            cCostQ.offer(cur);
            used.add(cur);
            ak++;
            bk++;
            cm++;
        }
        while(!aQ.isEmpty() && ak<k){
            int cur = aQ.poll();
            aCostQ.offer(cur);
            used.add(cur);
            ak++;
            cm++;
        }
        while(!bQ.isEmpty() &&  bk<k){
            int cur = bQ.poll();
            bCostQ.offer(cur);
            used.add(cur);
            bk++;
            cm++;
        }

        if(cm > m || ak<k || bk<k){
            System.out.println(-1);
            return;
        }


        while(cm<m && !cCostQ.isEmpty() && !aQ.isEmpty() && !bQ.isEmpty() && books[cCostQ.peek()][0]>=books[aQ.peek()][0]+books[bQ.peek()][0]){
            int toRemove = cCostQ.poll();
            used.remove(toRemove);
            int aNew = aQ.poll();
            int bNew = bQ.poll();
            aCostQ.offer(aNew);
            bCostQ.offer(bNew);
            used.add(aNew);
            used.add(bNew);
            cm++;
        }

        PriorityQueue<Integer> restQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0]));
        for(int i=0; i<n; i++){
            if(!used.contains(i)){
                restQ.offer(i);
            }
        }

        Queue<Integer> restCostQ = new LinkedList<>();
        while(cm<m && !restQ.isEmpty()){
            restCostQ.offer(restQ.poll());
            cm++;
        }

        if(cm < m){
            System.out.println(-1);
            return;
        }


        int res = 0;
        List<Integer> resQ = new ArrayList<>();
        while(!cCostQ.isEmpty()){
            int cur = cCostQ.poll();
            res += books[cur][0];
            resQ.add(cur);
        }
        while(!aCostQ.isEmpty()){
            int cur = aCostQ.poll();
            res += books[cur][0];
            resQ.add(cur);
        }
        while(!bCostQ.isEmpty()){
            int cur = bCostQ.poll();
            res += books[cur][0];
            resQ.add(cur);
        }
        while(!restCostQ.isEmpty()){
            int cur = restCostQ.poll();
            res += books[cur][0];
            resQ.add(cur);
        }
        System.out.println(res);
        for(int cur : resQ){
            System.out.print((cur+1)+" ");
        }
        System.out.println();
    }
}
