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

public class Main{

	public static void main(String[] args) throws IOException{
		Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int k=scan.nextInt();
        int time=0;
        int count=0;
        int aliceCount=0;
        int bobCount=0;
        while(n-->0)
        {
            int t=scan.nextInt();
            int alice=scan.nextInt();
            int bob=scan.nextInt();
            if(alice==0 && bob==0)
                continue;
            else
            {
                if(alice==1)
                    aliceCount++;
                if(bob==1)
                    bobCount++;
                time+=t;
            }
        }
        if(aliceCount>=k && bobCount>=k)
        {
            System.out.println(time);
        }
        else
            System.out.println(-1);
    }
}