import java.io.*;
import java.util.*;

public class E1 implements Comparable<E1>{
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
    public int t,a,b;
    public E1(int t,int a,int b){
        this.t=t;
        this.a=a;
        this.b=b;
    }
    @Override
    public int compareTo(E1 o) {
        // TODO Auto-generated method stub
        return toString().compareTo(o.toString());
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s=new FastReader();
        int n=s.nextInt();
        int k=s.nextInt();
        int counta=0,countb=0,countmain=0,count11=0,ans=0,ans2=0;
        E1[] arr = new E1[n];
        for(int i=0;i<n;i++){
            arr[i]=new E1(s.nextInt(),s.nextInt(),s.nextInt());
            if(arr[i].a==1&&arr[i].b==1) count11++;
            if(arr[i].a==1) counta++;
            if(arr[i].b==1) countb++;
        }
        if(counta<k||countb<k) ans=-1;
        else{
            Arrays.sort(arr, new Comparator<E1>() {
                @Override
                public int compare(E1 o1, E1 o2) {
                    return o1.t-o2.t;
                }
            });
            int i=0;counta=0;countb=0;
            while((counta<k&&countb<k)&&(arr[i].a==1&&arr[i].b==1)){
                ans2+=arr[i].t;
                counta++;countb++;
                i++;
            }
            i=0;
            while(counta<k||countb<k){
                if(arr[i].a==1&&arr[i].b==1){
                    counta++;
                    ans2+=arr[i].t;
                    countb++;
                }
                else if(counta<k&&(arr[i].a==1&&arr[i].b==0)){
                    counta++;
                    ans2+=arr[i].t;
                }
                else if(countb<k&&(arr[i].b==1&&arr[i].a==0)){
                    countb++;
                    ans2+=arr[i].t;
                }
                i++;
            }
            Arrays.sort(arr, new Comparator<E1>() {
                @Override
                public int compare(E1 o1, E1 o2) {
                    return (o2.a+o2.b)-(o1.a+o1.b);
                }
            });
            
            // System.out.println();
            // for(int i=0;i<n;i++){
            //     System.out.println(arr[i].t+" "+arr[i].a+" "+arr[i].b);
            // }
            i=0;counta=0;countb=0;
            while((counta<k&&countb<k)&&(arr[i].a==1&&arr[i].b==1)){
                ans+=arr[i].t;
                counta++;countb++;
                i++;
            }
            i=0;
            while(counta<k||countb<k){
                if(counta<k&&(arr[i].a==1&&arr[i].b==0)){
                    counta++;
                    ans+=arr[i].t;
                }
                else if(countb<k&&(arr[i].b==1&&arr[i].a==0)){
                    countb++;
                    ans+=arr[i].t;
                }
                i++;
            }
        }
        System.out.println(Math.min(ans,ans2));
    } 
}