import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 public class Reading_Books_hard_version
 {
 	static int []a,arr,b;
 	static String str="";
 	static int ptr01=0,ptr10=0,ptr11=0,ptr00=0,ptr=0;
 	static  Integer myInf = Integer.MAX_VALUE;
 	static int[]ans_arr,extra_array,extra_array1;
 	static int[]arr01,arr00,arr11,arr10,arr01_ind,arr10_ind,arr00_ind,arr11_ind;
 	static int taken00,taken01,taken11,taken10;
 	public static int smallest(int t1,int t2,int t3,int t4)
 	{
 		int s=(int)myInf,p=0;
 		if(t1<ptr01&&arr01[t1]<s)
 		 {s=arr01[t1];p=1;}
 		if(t2<ptr00&&arr00[t2]<s){s=arr00[t2];p=2;}
 		if(t3<ptr10&&arr10[t3]<s){s=arr10[t3];p=3;}
 		if(t4<ptr11&&arr11[t4]<s){s=arr11[t4];p=4;}
 		else{}
 		if(p==1){extra_array[ptr]=arr01_ind[taken01];taken01++;}
		else if(p==2){extra_array[ptr]=arr00_ind[taken00];taken00++;}
		else if(p==3){extra_array[ptr]=arr10_ind[taken10];taken10++;}
		else if(p==4){extra_array[ptr]=arr11_ind[taken11];taken11++;}
 		return s;
 	}
 	public static void main(String[]args)throws IOException
 	{
 		/*Scanner sc=new Scanner (System.in);
 		int n=sc.nextInt(),i;
 		int k=sc.nextInt();
 		arr=new int[n];
 		a=new int[n];
 		b=new int[n];
 		for(i=0;i<n;i++)
 		{
 			 arr[i]=sc.nextInt();
 			 a[i]=sc.nextInt();
 			 b[i]=sc.nextInt();
 		}*/
 		int i;
 		BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
 		String str=reader.readLine();
		 String[]array=str.split ("\\s+");
		 int n=Integer.parseInt(array[0]);
		 int m=Integer.parseInt(array[1]);
		 int k=Integer.parseInt(array[2]);
		 arr=new int[n];
 		a=new int[n];
 		b=new int[n];
 		
 		
		for(i=0;i<n;i++)
 		{
 			  str=reader.readLine();
 			 array=str.split ("\\s+");
 			 arr[i]=Integer.parseInt(array[0]);
 			 a[i]=Integer.parseInt(array[1]);
 			 b[i]=Integer.parseInt(array[2]);
 		}
 			
 		int ans=func(m,k);
 		if(ans==(int)myInf)
 			System.out.println("-1");
 		else 
 			System.out.println(ans);
 		//System.out.println("");
 		for(i=0;i<m;i++)
 			if(ans_arr[i]!=0)
 			System.out.print(ans_arr[i]+" ");
 	}
 	public static int func(int m,int k)
 	{
 		int n=arr.length,i;
 		//int[]extra_array;
 		ans_arr=new int [m];
 		//qsort_randomised(0,n-1);
 		/*for(i=0;i<n;i++)
 		{
 			System.out.print(arr[i]+" ");
 			System.out.print(a[i]+" ");
 			System.out.print(b[i]+" ");
 			System.out.println("");
 		}*/
 		
 		Boolean sort11=true,sort10=true,sort00=true,sort01=true;
 		int[]arr01w=new int[n];
 		int[]arr10w=new int[n];
 		int[]arr00w=new int[n];
 		int[]arr11w=new int[n];
 		arr01_ind=new int[n];
 		arr10_ind=new int[n];
 		arr11_ind=new int[n];
 		arr00_ind=new int[n];
 		for(i=0;i<n;i++)
 		{
 			if(a[i]==0&&b[i]==1)
 			{
 				arr01w[ptr01]=arr[i];arr01_ind[ptr01]=i;
 				ptr01++;
 				
 			}
 			if(a[i]==1&&b[i]==0)
 			{
 				arr10w[ptr10]=arr[i];arr10_ind[ptr10]=i;
 				ptr10++;
 				
 			}
 			if(a[i]==1&&b[i]==1)
 			{
 				arr11w[ptr11]=arr[i];arr11_ind[ptr11]=i;
 				ptr11++;
 				
 			}
 			if(a[i]==0&&b[i]==0)
 			{
 				arr00w[ptr00]=arr[i];arr00_ind[ptr00]=i;
 				ptr00++;
 				
 			}	
 		}
 		for(i=0;i<ptr01-1;i++)
 		{
 			if(arr01w[i]>arr01w[i+1])
			{
				sort01=false;
				break;
			}
 		}
 		for(i=0;i<ptr10-1;i++)
 		{
 			if(arr10w[i]>arr10w[i+1]){sort10=false;break;}
 		}
 		for(i=0;i<ptr11-1;i++)
 		{
 			if(arr11w[i]>arr11w[i+1]){sort11=false;break;}
 		}
 		for(i=0;i<ptr00-1;i++)
 		{
 			if(arr00w[i]>arr00w[i+1]){sort00=false;break;}
 		}
 		/*for(i=0;i<=ptr01;i++)
 		{
 			System.out.print(arr01w[i]+" hi01");
 		}
 		System.out.println("");
 		for(i=0;i<=ptr10;i++)
 		{
 			System.out.print(arr10w[i]+" hi10");
 		}
 		System.out.println("");
 		for(i=0;i<=ptr11;i++)
 		{
 			System.out.print(arr11w[i]+" hi11");
 		}
 		System.out.println("");
 		*/
 		arr01=new int[ptr01];
 		arr10=new int[ptr10];
 		arr11=new int[ptr11];
 		arr00=new int[ptr00];
 		
 		/*Arrays.fill(arr01,0);
 		Arrays.fill(arr10,0);
 		Arrays.fill(arr11,0);
 		Arrays.fill(arr00,0);*/
 		for(i=0;i<ptr01;i++)arr01[i]=arr01w[i];
 		for(i=0;i<ptr10;i++)arr10[i]=arr10w[i];
 		for(i=0;i<ptr11;i++)arr11[i]=arr11w[i];
 		for(i=0;i<ptr00;i++)arr00[i]=arr00w[i];
 		if(sort11==false){str="11";qsort_randomised(0,ptr11-1,arr11);}
 		if(!sort10){str="10";qsort_randomised(0,ptr10-1,arr10);}
 		if(!sort01){str="01";qsort_randomised(0,ptr01-1,arr01);}
 		if(!sort00){str="00";qsort_randomised(0,ptr00-1,arr00);}
 		/*for(i=0;i<ptr01;i++)
 		{
 			System.out.print(arr01[i]+" 01 "+arr01_ind[i]+" ");
 		}
 		System.out.println("");
 		for(i=0;i<ptr10;i++)
 		{
 			System.out.print(arr10[i]+" 10 "+arr10_ind[i]+ " ");
 		}
 		System.out.println("");
 		for(i=0;i<ptr11;i++)
 		{
 			System.out.print(arr11[i]+" 11 "+arr11_ind[i]+" ");
 		}
 		System.out.println("");
 		for(i=0;i<ptr00;i++)
 		{
 			System.out.print(arr00[i]+" 00 "+arr00_ind[i]+" ");
 		}*/
 		int j,j1,t00=0,t11=0,t01=0,t10=0;
 		int[]pre_sum01,pre_sum11,pre_sum10;
 		pre_sum01=new int[ptr01];
 		pre_sum11=new int[ptr11];
 		pre_sum10=new int[ptr10];
 		if(ptr01>0)
		{
			
			pre_sum01[0]=arr01[0];
			for(i=1;i<ptr01;i++)
			{
				pre_sum01[i]=pre_sum01[i-1]+arr01[i];
			}
		}
 		if(ptr10>0){
 			
			pre_sum10[0]=arr10[0];
			for(i=1;i<ptr10;i++)
			{
				pre_sum10[i]=pre_sum10[i-1]+arr10[i];
			}	
 		}	
 		if(ptr11>0)
 		{
 			
	 		pre_sum11[0]=arr11[0];
	 		for(i=1;i<ptr11;i++)
	 		{
	 			pre_sum11[i]=pre_sum11[i-1]+arr11[i];
	 		}	
 		}
 		int temp=0,min=(int)myInf,r=0,k1,s=0,a=0; 
 		ptr=0;
 		taken11=0;
 		taken01=0;
 		taken00=0;
 		taken10=0;
 		int extra=0,k2=0,q=0;
 		if(ptr11-1<k-1)q=ptr11-1;else q=k-1;
 		//for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" ");
 		for(i=q;i>=0&&i+2*(k-i-1)<=m;i--)
 		{
 			temp=0;
 			taken00=0;taken10=0;taken01=0;taken11=0;
 			ptr=0;
 			temp=temp+pre_sum11[i];
 			extra=0;
 			k1=i+1;
 			taken11=i+1;
 			if(k-k1>0)
 			{
 				if(ptr01>=k-k1&&ptr10>=k-k1)
 				{
 					if(2*(k-k1)+k1>m)return min;
 					temp=temp+pre_sum01[k-k1-1]+pre_sum10[k-k1-1];
 					/*if(m-k>0)
 					{

 					}*/
 					taken01=k-k1;
 					taken10=k-k1;
 				}
 				else
 					return min;
 				k2=k1+2*(k-k1);
 			}
 			else k2=k1; 
 			//System.out.print(" temp= "+temp);
 			//Arrays.fill(extra_array,0);
 			if(m-k2>0)
 			{
 				extra_array=new int [m-k2];
 				extra=0;
 				ptr=0;
 				for(a=0;a<m-k2;a++)
 				{
 					  s=smallest(taken01,taken00,taken10,taken11);
 					  //System.out.println("i= "+i+ " a= "+a+" small = "+s);
 					
 					ptr++;
 					extra=extra+s;
 					
 				}
 				//System.out.println("extra= "+extra);
 				//System.out.println("extra array");
 				//for(a=0;a<m-k2;a++)System.out.print(" "+extra_array[a]+ " ");
 			}
			temp=temp+extra;	
 			//System.out.println("temp= "+temp);
 			if(min>temp)
			{
				//System.out.println("yes");
				min=temp;
				ptr=0;
				for(j=0;j<=i;j++)
				{
					ans_arr[ptr]=arr11_ind[j]+1;
					ptr++;
				}
				for(j=0;j<=k-k1-1;j++)
				{
					ans_arr[ptr]=arr01_ind[j]+1;
					ptr++;
					ans_arr[ptr]=arr10_ind[j]+1;
					ptr++;
				}
				for(j=0;j<m-k2;j++)
				{
					ans_arr[ptr]=extra_array[j]+1;
					ptr++;
				}
			}
			
 		}
 		//for(i=0;i<m;i++)System.out.print(ans_arr[i]+" ");
 		taken00=0;taken10=0;taken01=0;taken11=0;ptr=0;extra=0;temp=0;
 		if(k<=ptr01&&k<=ptr10&&k*2<=m){
	 		temp=temp+pre_sum01[k-1]+pre_sum10[k-1];
	 		taken01=k;
	 		taken10=k;
			if(m>k*2)
			{
				extra_array1=new int[m-k*2];
				for(a=0;a<m-k*2;a++)
 				{
 					 s=smallest(taken01,taken00,taken10,taken11);
 					
 					
 					ptr++;
 					extra=extra+s;
 					
 				}
			}
			temp=temp+extra;
			//System.out.println("temp= "+temp);
	 		if(temp<min)	
			{
				min=temp;
				ptr=0;
				for(j=0;j<=k-1;j++)
				{
					ans_arr[ptr]=arr01_ind[j]+1;
					ptr++;
					ans_arr[ptr]=arr10_ind[j]+1;
					ptr++;
				}
				for(j=0;j<m-k*2;j++)
				{
					ans_arr[ptr]=extra_array1[j]+1;
					ptr++;
				}	
				
			}
 		}
 		/*for(i=k;i>0;i--)
 		{
 			 temp=0;
 			 if(ptr11>=k){
 			 	temp=pre_sum11[i-1];
				j1=i;

	
 			 }
 			 else
 			 {
 			 	
 			 	if(ptr11-1>=0&&ptr11-r-1>=0){
 			 		temp=pre_sum11[ptr11-r-1];
 			 		j1=ptr11;	
 			 		r++;
 			 	}
 			 	else
 			 	{
 			 		j1=0;
 			 	}
 			 	
 			 }
 			/*for(j=0;j<i;j++)
 			{
 				if(j>=ptr11)break;
 				temp=temp+arr11[j];
 			}*/
 			/*if(k-j1<ptr01||k-j1<ptr10)return temp;
 			if(k-j1!=0)
 			{
 				if(k-j1<ptr01||k-j1<ptr10)return -1;
 			}*/
 			//if(k-j1>ptr10||k-j1>ptr01)return min;
 			/*for(j=0;j<k-j1;j++)
 			{
 				/*if(j<ptr01||j<ptr10)
 				{
 					if(k-j1==0)
 					{
 						return temp;
 					}
 					else
 					{
 						return -1;
 					}
 				}
 				//if(j>=ptr01||j>=ptr10)return min;
 				temp=temp+arr01[j];
 				temp=temp+arr10[j];
 			}
 			if(k-j1>0){
	 			temp=temp+pre_sum01[k-j1-1];
	 			temp=temp+pre_sum10[k-j1-1];
 			}
 			if(temp<min)min=temp;
 		}*/
 		return min;
 	}
 	public static void qsort_randomised(int p,int r,int []arr)
 	{
 		if(p<r)
 		{
 			int q=random_partition(p,r,arr);
 			qsort_randomised(p,q-1,arr);
 			qsort_randomised(q+1,r,arr);
 		}

 	}
 	public static int random_partition(int p,int r,int[]arr)
 	{
 		int i1=(int)(Math.random()*(r-p));
 		int i=i1+p,temp1=0;
 		if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;}
 		else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;}
 		else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;}
 		else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;}
 		int temp=arr[i];
 		arr[i]=arr[r];
 		arr[r]=temp;
 		
 		 return partition(p,r,arr);
 	}
 	public static int partition(int p,int r,int[]arr)
 	{
 		int x=arr[r];
 		int j;
 		int i=p-1,temp1;
 		for(j=p;j<=r-1;j++)
 		{
 			if(arr[j]<=x)
 			{
 				i++;
 				int temp=arr[i];
 				arr[i]=arr[j];
 				arr[j]=temp;
 				if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;}
 		else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;}
 		else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;}
 		else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;}
 			}
 		}
 		int temp=arr[i+1];
 		arr[i+1]=arr[r];
 		arr[r]=temp;
 		if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;}
 		else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;}
 		else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;}
 		else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;}
 		return i+1;
 	}
 }
