    import java.io.*;
    import java.util.*;
    import java.math.*;
     
    public class bhaa {
     
        InputStream is;
        PrintWriter o;
        /////////////////// CODED++ BY++ ++ ++ ++ BHAVYA++ ARORA++ ++ ++ ++ FROM++ JAYPEE++ INSTITUTE++ OF++ INFORMATION++ TECHNOLOGY++ ////////////////
     
    ///////////////////////// Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. /////////////////
        class book implements Comparable<book>
        {
            long time;
            book(long t)
            {
                time=t;
            }
            public int compareTo(book b)
            {
                if(b.time==time)
                {
                    return 0;
                }
                else if(time<b.time)
                {
                    return -1;

                }
                else
                {
                    return 1;
                }
            }
            book(){};

        }
    
        
        void solve() {
            int n=ni();
            int k=ni();
            int tc1=0;
            int tc2=0;
        
            int ind1=0;
            int ind2=0;
            int ind3=0;
            long row1[]=new long[n];
            int row2[]=new int [n];
            int row3[]=new int[n];
            int ty1=0;
            int ty2=0;
            int ty3=0;
            for(int i=0;i<n;i++)
            {
                row1[i]=nl();
                row2[i]=ni();
                row3[i]=ni();
                if(row2[i]==1&&row3[i]==1)
                {
                    ty3++;
                }
                else if(row2[i]==1)
                {
                    ty1++;
                }
                else if(row3[i]==1) 
                {
                    ty2++;

                }
                
            }
            book ab[]=new book[ty1];
            book bb[]=new book[ty2];
            book mb[]=new book[ty3];

            for(int i=0;i<n;i++)
            {
                long time=row1[i];
                int al=row2[i];
                int bo=row3[i];
                if(al==1||bo==1)
                {

                if(al==1&&bo==1)
                {
                    tc1++;
                    tc2++;
                    mb[ind3++]=new book(time);
                }
                else if(al==1)
                {
                    tc1++;
                    ab[ind1++]=new book(time);
                }
                else if(bo==1)
                {
                    tc2++;
                    bb[ind2++]=new book(time);
                }
            }
                

            }

            if(tc1<k||tc2<k)
            {
                o.println(-1);
            }
            else
            {
                Arrays.sort(ab);
                Arrays.sort(bb);
                Arrays.sort(mb);
              /*  for(book b:ab)
                {
                    o.print(b.time+" ");
                }
                o.println();

                for(book b:bb)
                {
                    o.print(b.time+" ");
                }
                o.println();
                
                for(book b:mb)
                {
                    o.print(b.time+" ");
                }
                o.println();
                */
                int c1=k;
                int c2=k;
                int s1=ind1;
                int s2=ind2;
                int s3=ind3;
                int i1=0;
                int i2=0;
                int i3=0;
                long res=0;
                while(c1>0&&c2>0&&i1<s1&&i2<s2&&i3<s3)
                {
                    long indi=ab[i1].time+bb[i2].time;
                    long bot=mb[i3].time;
                    if(ind1<bot)
                    {
                        c1--;
                        c2--;
                        i1++;
                        i2++;
                        res+=indi;
                    }
                    else
                    {
                        c1--;
                        c2--;
                        i3++;
                        res+=bot;
                    }
                  // o.println("yes");


                }
                if(i3==s3)
                {
                    while(c1>0)
                    {
                        res+=ab[i1++].time;
                        c1--;


                    }
                    while(c2>0)
                    {
                        res+=bb[i2++].time;
                        c2--;
                    }
                }
                if(i1==s1)
                {

                    while(c1>0&&i3<s3)
                    {
                        c1--;
                        c2--;
                        res+=mb[i3++].time;
                       // o.println(res);


                    }
                    while(c2>0)
                    {
                        c2--;
                        res+=bb[i2++].time;
                    }

                }
               // o.println(res+"af");
                if(i2==s2)
                {
                    while(c2>0&&i3<s3)
                    {
                        c1--;
                        c2--;
                        res+=mb[i3++].time;


                    }
                    while(c1>0)
                    {
                        c1--;
                        res+=ab[i1++].time;
                    }

                }
                o.println(res);


                


            }
           
     
        
     
            
            
     
            
     
     
        }
     
     
     
     
     
     
       
     
     
        
     
     
     
     
     
     
     
     
     
     
     
     
        
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
        //---------- I/O Template ----------
        
        public static void main(String[] args) { new bhaa().run(); }
        void run() { 
            is = System.in; 
            o = new PrintWriter(System.out);
            solve();
            o.flush();
        }
        
        byte input[] = new byte[1024];
        int len = 0, ptr = 0;
        
        int readByte() { 
            if(ptr >= len) { ptr = 0; 
                try { len = is.read(input); } 
                catch(IOException e) { throw new InputMismatchException(); } 
                if(len <= 0) { return -1; } 
            } return input[ptr++];
        }
        boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
        int skip() { 
            int b = readByte(); 
            while(b != -1 && isSpaceChar(b)) { b = readByte(); } 
            return b;
        }
        
        char nc() { return (char)skip(); }
        String ns() { 
            int b = skip(); 
            StringBuilder sb = new StringBuilder(); 
            while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } 
            return sb.toString();
        }
        String nLine() { 
            int b = skip(); 
            StringBuilder sb = new StringBuilder(); 
            while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } 
            return sb.toString();
        }
        int ni() { 
            int n = 0, b = readByte(); 
            boolean minus = false; 
            while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
            if(b == '-') { minus = true; b = readByte(); } 
            if(b == -1) { return -1; }  //no input 
            while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
            return minus ? -n : n;
        }
        long nl() { 
            long n = 0L;    int b = readByte(); 
            boolean minus = false; 
            while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
            if(b == '-') { minus = true; b = readByte(); } 
            while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
            return minus ? -n : n;
        }
     
        double nd() { return Double.parseDouble(ns()); }
        float nf() { return Float.parseFloat(ns()); }
        int[] nia(int n) { 
            int a[] = new int[n]; 
            for(int i = 0; i < n; i++) { a[i] = ni(); } 
            return a;
        }
        long[] nla(int n) { 
            long a[] = new long[n]; 
            for(int i = 0; i < n; i++) { a[i] = nl(); } 
            return a;
        }
        int [][] nim(int n)
        {
            int mat[][]=new int[n][n];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    mat[i][j]=ni();
                }
            }
            return mat;
        }
        long [][] nlm(int n)
        {
            long mat[][]=new long[n][n];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    mat[i][j]=nl();
                }
            }
            return mat;
        }
        
     
     
     
     
        
        char[] ns(int n) { 
            char c[] = new char[n]; 
            int i, b = skip(); 
            for(i = 0; i < n; i++) { 
                if(isSpaceChar(b)) { break; } 
                c[i] = (char)b; b = readByte(); 
            } return i == n ? c : Arrays.copyOf(c,i);
        }
        void piarr(int arr[])
        {
            for(int i=0;i<arr.length;i++)
            {
                o.print(arr[i]+" ");
     
            }
            o.println();
        }
        void plarr(long arr[])
        {
            for(int i=0;i<arr.length;i++)
            {
                o.print(arr[i]+" ");
     
            }
            o.println();
        }
        
        void pimat(int mat[][])
        {
            for(int i=0;i<mat.length;i++)
            {
                for(int j=0;j<mat[0].length;j++)
                {
                    o.print(mat[i][j]+" ");
                }
                o.println();
            }
        }
        void plmat(long mat[][])
        {
            for(int i=0;i<mat.length;i++)
            {
                for(int j=0;j<mat[0].length;j++)
                {
                    o.print(mat[i][j]+" ");
                }
                o.println();
            }
     
        }
       static class DJset
{
    int n;
    ty1 arr[];
    class ty1
    {
        int rank;
        int par;
        ty1(int p,int r)
        {
            par=p;
            rank=r;
        }
    }
    DJset(int nu)
    {
        n=nu;
        arr=new ty1[n];
        for(int i=0;i<n;i++)
        {
            arr[i]=new ty1(i,1);
        }

    }
    int find(int x)
    {
        if(arr[x].par!=x)
        {
            arr[x].par=find(arr[x].par);
        }
        return arr[x].par;
    }
    void union(int x,int y)
    {
        x=find(x);
        y=find(y);
        if(x!=y)
        {
            if(arr[x].rank<arr[y].rank)
            {
                arr[x].par=y;
            }
            else if(arr[y].rank<arr[x].rank)
            {
                arr[y].par=x;
            }
            else
            {
                arr[x].rank++;
                arr[y].par=x;

            }
        }
    }
    public String toString()
    {
        String res="";
        for(int i=0;i<n;i++)
        {
            res+=(arr[i].par+" ");
        }
        
        return res;
    }
    public int countsets()
    {
        int res=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i].par==i)
            {
                res++;
            }
        }
        return res;
    }

    

    }
    
     
     
     
     
        //////////////////////////////////// template finished //////////////////////////////////////
     
     
     
    }

