import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.Array;
import java.util.*;

public class NTI {
    public static class pair implements Comparable<pair>{
        int time;
        int ind;
        public pair(int t, int i){
            this.time = t;
            this.ind = i;
        }

        @Override
        public int compareTo(pair o) {
            return this.time - o.time;
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(reader.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        ArrayList<pair> arr = new ArrayList<>();
        LinkedList<pair> both = new LinkedList<>();
        LinkedList<pair> alice = new LinkedList<>();
        LinkedList<pair> bob = new LinkedList<>();
        for(int i = 0; i < n; ++i){
            st = new StringTokenizer(reader.readLine());
            int t = Integer.parseInt(st.nextToken());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            arr.add(new pair(t,i + 1));
            if(a == 1 && b == 1){
                both.add(new pair(t,i + 1));
            }
            else if(a == 1){
                alice.add(new pair(t,i + 1));
            }
            else if(b == 1){
                bob.add(new pair(t,i + 1));
            }
        }
        Collections.sort(arr);
        Collections.sort(both);
        Collections.sort(alice);
        Collections.sort(bob);
        if(both.size() + Math.min(alice.size(), bob.size()) < k){
            writer.println(-1);
            writer.close();
            return;
        }
        TreeSet<Integer> used = new TreeSet<>();
        long ans = 0;
        for(int i = 0; i < k; ++i){
            if(both.size() > 0 && alice.size() > 0 && bob.size() > 0){
                if(both.peekFirst().time <= Math.max(alice.peekFirst().time, bob.peekFirst().time) || used.size() + 2 > m){
                    ans += both.peekFirst().time;
                    used.add(both.peekFirst().ind);
                    both.removeFirst();
                }
                else{
                    ans += alice.peekFirst().time;
                    used.add(alice.peekFirst().ind);
                    alice.removeFirst();
                    ans += bob.peekFirst().time;
                    used.add(bob.peekFirst().ind);
                    bob.removeFirst();
                }
            }
            else if(both.size() > 0){
                ans += both.peekFirst().time;
                used.add(both.peekFirst().ind);
                both.removeFirst();
            }
            else if(used.size() + 2 <= m){
                ans += alice.peekFirst().time;
                used.add(alice.peekFirst().ind);
                alice.removeFirst();
                ans += bob.peekFirst().time;
                used.add(bob.peekFirst().ind);
                bob.removeFirst();
            }
            else{
                writer.println(-1);
                writer.close();
                return;
            }
        }
        for(int i = 0; i < n && used.size() < m; ++i){
            if(!(used.contains(arr.get(i).ind))){
                ans += arr.get(i).time;
                used.add(arr.get(i).ind);
            }
        }
        writer.println(ans);
        StringBuilder s = new StringBuilder();
        for(Integer ii : used){
            s.append(ii);
            s.append(" ");
        }
        writer.println(s);
        writer.close();
    }
}