import java.util.*;
import java.io.*;
public class e2 {
	public static void main(String[] args) throws IOException {
		FastScanner sc = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
		ArrayList<Info> both = new ArrayList<>();
		ArrayList<Info> alice = new ArrayList<>();
		ArrayList<Info> bob = new ArrayList<>();
		ArrayList<Info> all = new ArrayList<>();
		
		for (int i = 0 ; i < n ; i++) {
			int time = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
			
			if (a == 1 && b == 1) {
				both.add(new Info(time,i));
			} else if (a == 1) {
				alice.add(new Info(time,i));
			} else if (b == 1) {
				bob.add(new Info(time,i));
			}
			
			all.add(new Info(time, i));
		}
		
		if (bob.size() + both.size() < k || alice.size() + both.size() < k || all.size() < m) {
			out.println(-1);
			out.close();
			return;
		}

		Collections.sort(both);
		Collections.sort(alice);
		Collections.sort(bob);
		Collections.sort(all);
		
		HashSet<Integer> seen = new HashSet<>();
		
		int ans = 0;
		int liked = 0;
		for (int i = 0 ; i < both.size() && i < k; i++) {
			ans += both.get(i).time;
			seen.add(both.get(i).index);
			liked++;
		}
		
		int bothPtr = liked-1;
		
		for (int i = 0 ; i < alice.size() && i < bob.size() && seen.size() < m; i++) {
			if (liked < k) {
				ans += alice.get(i).time;
				ans += bob.get(i).time;
				
				seen.add(alice.get(i).index);
				seen.add(bob.get(i).index);
				
				liked++;
			} else {
				if (bothPtr < 0) {
					break;
				}
				
				if (alice.get(i).time + bob.get(i).time < both.get(bothPtr).time) {
					ans -= both.get(bothPtr).time;
					ans += alice.get(i).time;
					ans += bob.get(i).time;
					
					seen.remove(both.get(bothPtr).index);
					seen.add(alice.get(i).index);
					seen.add(bob.get(i).index);
					
					bothPtr--;
				} else {
					break;
				}
			}
		}
		
		for (int i = 0 ; i < all.size() && seen.size()<m ; i++) {
			if (!seen.add(all.get(i).index)) continue;
			
			ans += all.get(i).time;
		}
		
		if (seen.size() > m) {
			out.println(-1);
			out.close();
			return;
		}
		
		
		out.println(ans);
		
		for (Integer e : seen) {
			out.print((e+1) + " ");
		}
		
		out.println();
		
		out.close();
	}

	static class Info implements Comparable<Info>{
		int time, index;
		
		public Info(int t, int i) {
			time=t; index=i;
		}

		@Override
		public int compareTo(Info o) {
			return time-o.time;
		}
	}
	
	static class FastScanner {
	    BufferedReader br;
	    StringTokenizer st;
		
	    public FastScanner(InputStream i) {
	        br = new BufferedReader(new InputStreamReader(i));
	        st = new StringTokenizer("");
	    }
				
	    public String next() throws IOException {
	        if(st.hasMoreTokens())
	            return st.nextToken();
	        else
	            st = new StringTokenizer(br.readLine());
	        return next();
	    }

	    public int nextInt() throws IOException {
	        return Integer.parseInt(next());
	    }
	    public long nextLong() throws IOException {
	        return Long.parseLong(next());
	    }
	    public double nextDouble() throws IOException {
	        return Double.parseDouble(next());
	    }
	}
}
