

import java.util.*;
import java.io.*;
//Captain on duty!



public class Main {

    static void compare(Main.pair a[], int n) {

        Arrays.sort(a, new Comparator<Main.pair>() {
            @Override
            public int compare(Main.pair p1, Main.pair p2) {
                return p1.f - p2.f;
            }
        });
    }

    public static boolean checkPalindrome(String s) {
        // reverse the given String
        String reverse = new StringBuffer(s).reverse().toString();

        // check whether the string is palindrome or not
        if (s.equals(reverse))
            return true;

        else
            return false;
    }

    static class pair implements Comparable {
        int f;
        int s;

        pair(int fi, int se) {
            f = fi;
            s = se;
        }

        public int compareTo(Object o)//desc order
        {
            pair pr = (pair) o;
            if (s > pr.s)
                return -1;
            if (s == pr.s) {
                if (f > pr.f)
                    return 1;
                else
                    return -1;
            } else
                return 1;
        }

        public boolean equals(Object o) {
            pair ob = (pair) o;
            if (o != null) {
                if ((ob.f == this.f) && (ob.s == this.s))
                    return true;
            }
            return false;
        }

        public int hashCode() {
            return (this.f + " " + this.s).hashCode();
        }


    }

    public static boolean palin(int l, int r, char[] c) {
        while (l <= r) {
            if (c[l] != c[r]) return false;
            l++;
            r--;
        }
        return true;
    }


    public static long gcd(long a, long b) {
        if (b == 0)
            return a;

        return gcd(b, a % b);
    }
    public static long lcm(long a, long b)
    {
        return (a*b)/gcd(a, b);
    }

    public static long hcf(long a, long b) {
        long t;
        while (b != 0) {
            t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    public static boolean isPrime(long n) {
        if (n <= 1)
            return false;

        // Check from 2 to n-1
        for (int i = 2; i <= Math.sqrt(n) + 1; i++)
            if (n % i == 0)
                return false;

        return true;
    }

    public static String reverse(String str) {
        String str1 = "";
        for (int i = 0; i < str.length(); i++) {
            str1 = str1 + str.charAt(str.length() - i - 1);
        }
        return str1;
    }


    public static double fact(long a) {
        if (a == 1)
            return 1;
        else
            return a * fact(a - 1);
    }

    static boolean isPerfectSquare(double x)
    {

        // Find floating point value of
        // square root of x.
        double sr = Math.sqrt(x);

        // If square root is an integer
        return ((sr - Math.floor(sr)) == 0);
    }


    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }


    }


    public static void main(String[] args) {
        FastReader s = new FastReader();
        //System.out.println("Forces Babyy!!");


        int n=s.nextInt();
        int k=s.nextInt();
        int[] time=new int[n];
        int[] a=new int[n];
        int[] b=new int[n];
        for(int i=0;i<n;i++)
        {
            time[i]=s.nextInt();
            a[i]=s.nextInt();
            b[i]=s.nextInt();
        }
        int a_ct=0;
        int b_ct=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]==1)
                a_ct++;
            if(b[i]==1)
                b_ct++;
        }
        if(a_ct<k || b_ct<k)
            System.out.println(-1);
        else
        {
            ArrayList A=new ArrayList();
            ArrayList B=new ArrayList();

            for(int i=0;i<n;i++)
            {
                if(a[i]==1)
                    A.add(time[i]);
                if(b[i]==1)
                    B.add(time[i]);
            }

            Collections.sort(A);
            Collections.sort(B);

            int[] a_vis=new int[200000];
            int[] b_vis=new int[200000];

            for(int i=0;i<A.size();i++)
                a_vis[(int)A.get(i)]++;

            for(int i=0;i<B.size();i++)
                b_vis[(int)B.get(i)]++;
            long ans1= Long.MAX_VALUE;
            long ans2=ans1;



            //Taking all A's
            long alice_time=0;
            long bob_time=0;
            int c=0;
            int[] came=new int[200000];
            for(int i=0;i<k;i++)
            {
                alice_time+=(int)A.get(i);
                if(b_vis[(int)A.get(i)]>0)
                {
                    c++;
                    b_vis[(int)A.get(i)]--;
                    came[(int)A.get(i)]++;
                    bob_time+=(int)A.get(i);
                }
            }
            if(c==k)
                ans1=(alice_time);
            else
            {
                int i=0;
                long bob_extra_time=0;
                while(c!=k)
                {
                    if(came[(int)B.get(i)]>0)
                        came[(int)B.get(i)]--;
                    else
                    {
                        bob_extra_time+=((int)B.get(i));
                        c++;
                    }
                    i++;

                }
                //System.out.println(alice_time + " "  + bob_time);
                bob_time+=bob_extra_time;
                ans1=alice_time+bob_extra_time;
                //System.out.println(ans1);

            }


            int[] a_vis1=new int[200000];
            int[] b_vis1=new int[200000];

            for(int i=0;i<A.size();i++)
                a_vis1[(int)A.get(i)]++;

            for(int i=0;i<B.size();i++)
                b_vis1[(int)B.get(i)]++;
            //Taking all B's
            long alice_time1=0;
            long bob_time1=0;
            int c1=0;
            int[] came1=new int[200000];
            for(int i=0;i<k;i++)
            {
                bob_time1+=(int)B.get(i);
                if(a_vis1[(int)B.get(i)]>0)
                {
                    c1++;
                    a_vis1[(int)B.get(i)]--;
                    came1[(int)B.get(i)]++;
                    alice_time1+=(int)B.get(i);
                }
            }
            if(c1==k)
                ans2=(bob_time1);
            else
            {
                int i=0;
                long alice_extra_time=0;
                while(c1!=k)
                {
                    if(came1[(int)A.get(i)]>0)
                        came1[(int)A.get(i)]--;
                    else
                    {
                        alice_extra_time+=((int)A.get(i));
                        c1++;
                    }
                    i++;

                }
                //System.out.println(alice_time + " "  + bob_time);
                alice_time1+=alice_extra_time;
                ans2=bob_time1+alice_extra_time;
                //System.out.println(ans2);

            }


            System.out.println(Math.min(ans1,ans2));

        }




    }

}
