import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 public class Reading_Books_easy_version
 {
 	static int []a,arr,b;
 	static  Integer myInf = Integer.MAX_VALUE;

 	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 k=Integer.parseInt(array[1]);
		 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(k);
 		if(ans==(int)myInf)
 			System.out.println("-1");
 		else 
 			System.out.println(ans);
 	}
 	public static int func(int k)
 	{
 		int n=arr.length,i;
 		//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("");
 		}*/
 		int ptr01=0,ptr10=0,ptr11=0;
 		int[]arr01w=new int[n];
 		int[]arr10w=new int[n];
 		int[]arr11w=new int[n];
 		for(i=0;i<n;i++)
 		{
 			if(a[i]==0&&b[i]==1)
 			{
 				arr01w[ptr01]=arr[i];
 				ptr01++;
 			}
 			if(a[i]==1&&b[i]==0)
 			{
 				arr10w[ptr10]=arr[i];
 				ptr10++;
 			}
 			if(a[i]==1&&b[i]==1)
 			{
 				arr11w[ptr11]=arr[i];
 				ptr11++;
 			}
 				
 		}
 		/*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("");
 		*/
 		int[]arr01=new int[ptr01];
 		int[]arr10=new int[ptr10];
 		int[]arr11=new int[ptr11];
 		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];
 		qsort_randomised(0,ptr11-1,arr11);
 		qsort_randomised(0,ptr10-1,arr10);
 		qsort_randomised(0,ptr01-1,arr01);
 		/*for(i=0;i<ptr01;i++)
 		{
 			System.out.print(arr01[i]+" hi011");
 		}
 		System.out.println("");
 		for(i=0;i<ptr10;i++)
 		{
 			System.out.print(arr10[i]+" hi101");
 		}
 		System.out.println("");
 		for(i=0;i<ptr11;i++)
 		{
 			System.out.print(arr11[i]+" hi111");
 		}
 		System.out.println("");
 		*/int j,j1;
 		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];
	 		}	
 		}
 		
 		//for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" ");
 		
 		int temp=0,min=(int)myInf;
 		for(i=k;i>0;i--)
 		{
 			 temp=0;
 			 if(ptr11>=k){
 			 	temp=pre_sum11[i-1];
				j1=i;

	
 			 }
 			 else
 			 {
 			 	if(ptr11-1>=0){
 			 		temp=pre_sum11[ptr11-1];
 			 		j1=ptr11;	
 			 	}
 			 	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;
 		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;
 		for(j=p;j<=r-1;j++)
 		{
 			if(arr[j]<=x)
 			{
 				i++;
 				int temp=arr[i];
 				arr[i]=arr[j];
 				arr[j]=temp;
 			}
 		}
 		int temp=arr[i+1];
 		arr[i+1]=arr[r];
 		arr[r]=temp;
 		return i+1;
 	}
 }
