import java.util.*;

public class ReadBooks {
	public static class Node{
		long val;
		int index;
		Node(long val, int index){
			this.val = val;
			this.index = index;
		}
	}
	static boolean insertValIntoList(LinkedList<Node> adj, Node valu,int k, long[] sum, int idx, boolean visit[]) {
		int i=0;
		if(adj.size()==0) {
			adj.add(valu);
			visit[valu.index] = true;
			sum[idx] = sum[idx] + valu.val;
			return true;
		}
		else if(adj.size() < k && valu.val >= adj.getLast().val) {
			adj.add(valu);
			sum[idx] = sum[idx] + valu.val;
			return true;
		}
		else {
			for(Node elm: adj) {
				if(valu.val < elm.val) {
					adj.add(i,valu);
					visit[valu.index] = true;
					if(adj.size() > k) {
						sum[idx] = sum[idx] - adj.getLast().val+ valu.val;
						visit[adj.getLast().index] = false;
						adj.removeLast();
					}else {
						sum[idx] = sum[idx] + valu.val;
					}
					return true;
				}
				i++;
			}
		}
		return false;
	}
	
	static boolean insertCommonValIntoList(LinkedList<Node> adjA, LinkedList<Node> adjB, LinkedList<Node> adjC, long[] sum, Node val, int k, boolean visit[]) {
		
		if(adjA.size() + adjC.size() < k && adjB.size() + adjC.size() == k ) {
			insertValIntoList(adjC,val,k,sum, 2, visit);
			sum[1] = sum[1] - adjB.getLast().val;
			visit[adjB.getLast().index] = false;
			adjB.removeLast();
			return true;
		}
		else if(adjA.size() + adjC.size() == k && adjB.size() + adjC.size() < k ) {
			insertValIntoList(adjC,val,k,sum, 2, visit);
			sum[0] = sum[0] - adjA.getLast().val;
			visit[adjA.getLast().index] = false;
			adjA.removeLast();
			return true;
		}
		else if(adjA.size() + adjC.size() < k && adjB.size() + adjC.size() < k) {
			insertValIntoList(adjC,val,k,sum, 2, visit);
			return true;
		}
		else {
			long sum1 = sum[0]+sum[1]+sum[2];
			long sum2 = sum1 - (adjA.size()==0?0:adjA.getLast().val) - (adjB.size()==0?0:adjB.getLast().val) + val.val;
			long sum3 = sum1 - (adjC.size()==0?0:adjC.getLast().val) + val.val;
			//System.out.println(">>>>>>>>>>> sum2: "+ sum2 +" sum3: "+ sum3 +" <<<<<<<<<<<<<");
			if(sum1 > sum2 || sum1 > sum3) {
				if(sum2 < sum3) {
					sum[0] -= adjA.getLast().val;
					visit[adjA.getLast().index] = false;
					adjA.removeLast();
					sum[1] -= adjB.getLast().val;
					visit[adjB.getLast().index] = false;
					adjB.removeLast();
					insertValIntoList(adjC,val,k, sum,2, visit);
					return true;
				}else {
					sum[2] -= adjC.getLast().val;
					visit[adjC.getLast().index] = false;
					adjC.removeLast();
					insertValIntoList(adjC,val,k, sum,2, visit);
					return true;
				}
			}
			else {
				return false;
			}
		}
	}

	public static void main(String[] args) {
		//System.out.println("hello world");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String list[] = str.split(" ");
		int n = Integer.parseInt(list[0]);
		int m = Integer.parseInt(list[1]);
		int k = Integer.parseInt(list[2]);
		
		if(n<m || m<k) {
			System.out.println(-1);
		}else {			
			Node arr[][] = new Node[n][3];
			for(int i=0;i<n;i++) {
				str = sc.nextLine();
				list = str.split(" ");
				arr[i][0] = new Node(Long.parseLong(list[0]), i);
				arr[i][1] = new Node(Long.parseLong(list[1]), i);
				arr[i][2] = new Node(Long.parseLong(list[2]), i);
			}
			LinkedList<Node> adjA = new LinkedList<>();
			LinkedList<Node> adjB = new LinkedList<>();
			LinkedList<Node> adjC = new LinkedList<>();
			boolean visit[] = new boolean[n];
			Arrays.fill(visit, false);
			long sum[] = new long[4]; 
			Arrays.fill(sum, 0);
			for(int i=0;i<n;i++) {
				if(arr[i][1].val==1 && arr[i][2].val==0) {
					insertValIntoList(adjA, arr[i][0],k,sum,0, visit);
				}
				else if(arr[i][1].val==0 && arr[i][2].val==1) {
					insertValIntoList(adjB, arr[i][0],k,sum,1, visit);
				}
				
				/*
				 * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val +
				 * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node
				 * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println("");
				 * System.out.println("sumA: "+sum[0]+"  sumB: "+sum[1]);
				 * System.out.println(""); System.out.print("Visit : "); for(int
				 * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], ");
				 * }else { System.out.print(visit[j] + ", "); } } System.out.println("");
				 * System.out.println("======================");
				 */
				 
			}
			//System.out.println("==========TRIM LOOP============");
			if(adjA.size() + adjB.size()>m) {
				for(int i=0;i<(adjA.size() + adjB.size() - m);i++) {
					if(adjA.getLast().val > adjB.getLast().val) {
						visit[adjA.getLast().index] = false;
						sum[0] -= adjA.getLast().val;
						adjA.removeLast();
						
					}else {
						visit[adjB.getLast().index] = false;
						sum[1] -= adjB.getLast().val;
						adjB.removeLast();
					}
					
					/*
					 * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val +
					 * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node
					 * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println("");
					 * System.out.println("sumA: "+sum[0]+"  sumB: "+sum[1]);
					 * System.out.println(""); System.out.print("Visit : "); for(int
					 * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], ");
					 * }else { System.out.print(visit[j] + ", "); } } System.out.println("");
					 * System.out.println("======================");
					 */
				}
			}
			//System.out.println("==========2ND LOOP============");
			for(int i=0;i<n;i++) {
				if(arr[i][1].val==1 && arr[i][2].val==1) {
					insertCommonValIntoList(adjA,adjB, adjC,sum, arr[i][0],k, visit);
				
					
					/*
					 * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val +
					 * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node
					 * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println("");
					 * System.out.print("adjC : "); for(Node elm: adjC) { System.out.print(elm.val +
					 * " --> "); } System.out.println("");
					 * System.out.println("sumA: "+sum[0]+"  sumB: "+sum[1]+"  sumC: "+sum[2]);
					 * System.out.println(""); System.out.print("Visit : "); for(int
					 * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], ");
					 * }else { System.out.print(visit[j] + ", "); } } System.out.println("");
					 * System.out.println("======================");
					 */
					 
				}
			}
			if(adjA.size()+adjB.size()+adjC.size() > m) {
				System.out.println(-1);
			}else if(adjA.size()+adjC.size() < k || adjB.size()+adjC.size() < k) {
				System.out.println(-1);
			}
			else {
			//System.out.println("==========3RD LOOP============");
				LinkedList<Node> adjD = new LinkedList<>();
				int read = adjA.size() + adjB.size() + adjC.size();
				if(m-read != 0) {
					for(int i=0;i < n;i++) {
						if(visit[arr[i][0].index] == false) {
							insertValIntoList(adjD,arr[i][0],m-read,sum,3,visit);
						}
						
						/*
						 * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val +
						 * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node
						 * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println("");
						 * System.out.print("adjC : "); for(Node elm: adjC) { System.out.print(elm.val +
						 * " --> "); } System.out.println(""); System.out.print("adjD : "); for(Node
						 * elm: adjD) { System.out.print(elm.val + " --> "); } System.out.println("");
						 * System.out.println("sumA: "+sum[0]+"  sumB: "+sum[1]+"  sumC: "+sum[2]
						 * +"  sumD: "+sum[3]); System.out.println(""); System.out.print("Visit : ");
						 * for(int j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] +
						 * "], "); }else { System.out.print(visit[j] + ", "); } }
						 * System.out.println(""); System.out.println("======================");
						 */
						 
					}
				}
				System.out.println(sum[0]+sum[1]+sum[2]+sum[3]);
				if(!adjA.isEmpty()) {
					for(Node node: adjA) {
						System.out.print((node.index+1) + " ");
					}					
				}
				if(!adjB.isEmpty()) {
					for(Node node: adjB) {
						System.out.print((node.index+1) + " ");
					}					
				}
				if(!adjC.isEmpty()) {
					for(Node node: adjC) {
						System.out.print((node.index+1) + " ");
					}					
				}
				if(!adjD.isEmpty()) {
					for(Node node: adjD) {
						System.out.print((node.index+1) + " ");
					}					
				}
			}
		}
	}
}
