import java.util.Scanner;


public class ReadingBookVersionEasy {
 
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n,k,min=10000000,dem=0,demb=0;
        long s=0;
        n = scanner.nextInt();
        int[] t = new int[n];
        int[] b = new int[n];
        int[] b0 = new int[n];
        int[] b1 = new int[n];
        int[] c = new int[n];
        int[] c0 = new int[n];
        int[] c1 = new int[n];
     
        k = scanner.nextInt();
        int m0=0,m1=0;
        for (int i=0;i<n;i++){
            t[i] = scanner.nextInt();
            b[i] = scanner.nextInt();
            c[i] = scanner.nextInt();
            if (b[i]==1) {
                b1[m1]=t[i];
              if (c[i]==1) c1[m1]=t[i]; else c1[m1]=0;
            m1++;  
        } else if (b[i]==0 && c[i]!=0)
        {
                b0[m0]=0;
                if (c[i]==1) c0[m0]=t[i]; else c0[m0]=0;
            m0++;  
            }
           
        }
        
        sortb(b1,c1,0,m1-1);
        sortc(b0,c0,0,m0-1);
       //main
       if (m1<k) System.out.println("-1"); else
       {
           for (int i=0;i<m1;i++){
               if (c1[i]!=0){
                   s=s+c1[i];
                   dem++;
                   demb++;
                   if (dem==k) break;
               }
           }
           if (demb<k){
               for (int i=0;i<m1;i++){
                   if (b1[i]!=0 && c1[i]==0){
                       s=s+b1[i];
                   demb++;
                   if (demb==k) break;
               }
           }
       }
       
       if (dem>=k && demb>=k) System.out.println(s); else
       {
           if (dem<k){
               for (int i=0;i<m0;i++)
                   if (c0[i]!=0){
                       s=s+c0[i];
                       dem++;
                       if (dem==k) break;
               }
               System.out.println(s);
           }
       }
       }
       
    }
 
    public static void sortb(int b[],int c[],int i,int j){
        int dau,cuoi,giua;
        if (i<j){
            dau=i;
            cuoi=j;
            giua = b[(dau+cuoi)/2];
             while (i<j)
             {
                 while (b[i]<giua) i++;
                 while (b[j]>giua) j--;
                 if (i<=j){
                     swap(b,i,j);
                     swap(c,i,j);
                     i++;
                     j--;
                 }
             }
             sortb(b,c,dau,j);
             sortb(b,c,i,cuoi);
            
            
        }
    }
    
    public static void sortc(int b[],int c[],int i,int j){
        int dau,cuoi,giua;
        if (i<j){
            dau=i;
            cuoi=j;
            giua = c[(dau+cuoi)/2];
             while (i<j)
             {
                 while (c[i]<giua) i++;
                 while (c[j]>giua) j--;
                 if (i<=j){
                     swap(b,i,j);
                     swap(c,i,j);
                     i++;
                     j--;
                 }
             }
             sortc(b,c,dau,j);
             sortc(b,c,i,cuoi);
            
            
        }
    }
    
    public static void swap(int[] arr,int i,int j) { 
     int temp; 
     temp=arr[i]; 
     arr[i]=arr[j]; 
     arr[j]=temp; 
    } 

    
    
 
    
    
}

