

import java.util.Scanner;

public class CriptografiaCesar {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(),kt=0,t=0,newn=0,x = 0,mt=0;
		int [][] l = new int[n][3],nl = new int[n][3];
		String[] nls = new String[n];
		int [] nlt = new int[n];
		String uses = "";
		for(int i = 0;i<n;i++) {
			l[i][0] = in.nextInt();
			l[i][1] = in.nextInt();
			l[i][2] = in.nextInt();
		}
		
		for(int i=0;i<n;i++) {
			if(l[i][1] == 1 && l[i][2] == 1) {
				nl[x][0]=l[i][0];
				nl[x][1]=1;
				nl[x][2]=1;
				nls[x]=(i+1)+" ";
				nlt[x]=1;
				x++;
			}
		}
		
		while(tiverpares(n,l)) {
		int a=-1,b=-1;
		for(int i = 0;i<n;i++) {
			if(l[i][1] == 1 && l[i][2]==0) {
				a=i;
			}
			if(l[i][1] == 0 && l[i][2]==1) {
				b=i;
			}
		}
		for(int i = 0;i<n;i++) {
			if(l[i][0]<l[a][0] && l[i][1] == 1 && l[i][2] == 0) {
				a=i;
			}
		}	
		for(int i = 0;i<n;i++) {
			if(l[i][0]<l[a][0] && l[i][1] == 0 && l[i][2] == 1) {
					b=i;
			}
		}	
		
		if(a!=-1 && b!=-1) {
			nl[x][0]=l[a][0]+l[b][0];
			nl[x][1]=1;
			nl[x][2]=1;
			nls[x]=(a+1)+" "+(b+1)+" ";
			nlt[x]=2;
			x++;
			l[a]=new int[3];
			l[b]=new int[3];
		}
		
		}		
		
		while(kt<k) {
			int tt=0,index=-1;
			for(int i = 0;i<n;i++) {
				if(nl[i][0]>tt && nl[i][1]==1 && nl[i][2]==1) {
					tt=nl[i][0];
					index=i;
					break;
				}
			}
			for(int i = 0;i<n;i++) {
				if(nl[i][0]<tt && nl[i][1]==1 && nl[i][2]==1 && mt+nlt[i]<=m) {
					tt=nl[i][0];
					index=i;
				}
			}
			
			if(tt > 0 && index> -1 && mt+nlt[index]<=m) {
				t+=nl[index][0];
				uses+=nls[index];
				nl[index]=new int[3];
				kt++;
				mt+=nlt[index];				
			}else {
				break;
			}
			
		}

		while(mt<m) {
			int tt=0,index=-1;
			do {
			tt=0;index=-1;
			for(int i=0;i<n;i++) {
				if(l[i][0] > tt) {
					tt=l[i][0];
					index=i;
					break;
				}
			}
			for(int i=0;i<n;i++) {
				if(l[i][0] < tt && l[i][0] > 0) {
					tt=l[i][0];
					index=i;
				}
			}
			if(naotem(uses,index)) {
				break;
			}else {
				l[index]=new int[3];
			}
			}while(true);
			if(naotem(uses,index)) {
			uses+=(index+1)+" ";
			t+=tt;
			mt++;
			}else {
				break;
			}
		}
	
		if(kt>=k && mt==m && !tem(uses)) {
			System.out.println(t);
			System.out.println(uses);	
		}else {
			System.out.println(-1);
		}
		
	}

	private static boolean naotem(String uses, int index) {
		for(int i=0;i<uses.length()/2;i++) {
			if(uses.charAt(i*2) == (char)((index+1)+'0')) {
				return false;
			}
		}
		return true;
	}
	
	private static boolean tem(String uses) {
		for(int i=0;i<uses.length()/2;i++) {
			if(uses.charAt(i*2) == '-') {
				return true;
			}
		}
		return false;
	}

	private static boolean tiverpares(int n, int[][] l) {
		boolean a =false,b=false;
		for(int i = 0;i<n;i++) {
			if(l[i][1] == 1 && l[i][2]==0) {
				a=true;
			}
			if(l[i][1] == 0 && l[i][2]==1) {
				b=true;
			}
		}
		if(a && b) {
			return true;
		}
		return false;
	}

	
}
