import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class code6 {

	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 static void main(String[] args) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		FastReader scn = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		int t = 1;
		//int p = t;
		while(t-->0) {
			int n = scn.nextInt();
			int k = scn.nextInt();
			Pair[] arr = new Pair[n];
			int c1 = 0;
			int c2 = 0;
			for(int i=0; i<n; i++) {
				int a = scn.nextInt();
				int b = scn.nextInt();
				int c = scn.nextInt();
				c1+=b;
				c2+=c;
				arr[i] = new Pair(i,a,b,c);
			}
			Arrays.sort(arr);
			LinkedList<Pair> q1 = new LinkedList<>();
			LinkedList<Pair> q2 = new LinkedList<>();
			if(c1<k || c2<k) {
				System.out.println("-1");
				continue;
			}
			long ans = 0;
			int s = k;
			for(int i=0; i<n; i++) {
				int flag = 0;
//				if((q1.size()<s && q2.size()<s) && arr[i].a==1 && arr[i].b == 1)
//					s--;
//				if(!q1.isEmpty() && !q2.isEmpty() && q1.size() == s && q2.size() == s && arr[i].a == 1 && arr[i].b == 1) {
//					int a = q1.getLast().t + q2.getLast().t;
//					if(a>arr[i].t) {
//						ans-=a;
//						ans+=arr[i].t;
//						s--;
//						q1.removeLast();
//						q2.removeLast();
//					}
//				}
//				if(q1.size()<s && arr[i].a == 1) {
//					if(arr[i].b != 1)
//						q1.addLast(arr[i]);
//					ans += arr[i].t;
//				}
//				if(q2.size()<s && arr[i].b == 1 && arr[i].a != 1) {
//					q2.addLast(arr[i]);
//					ans+=arr[i].t;
//				}
				if(q1.size() == k && q2.size() == k && arr[i].a==1 && arr[i].b == 1) {
					Pair p1 = q1.getLast();
					Pair p2 = q2.getLast();
					if((p1.a == 1 && p1.b == 1) || (p2.a == 1 && p2.b == 1))
						continue;
					int a = p1.t + p2.t;
					if(a>arr[i].t) {
						ans -= a;
						ans += arr[i].t;
						q1.removeLast();
						q2.removeLast();
						q1.addFirst(arr[i]);
						q2.addFirst(arr[i]);
					}
					continue;
				}
				
				if(q1.size() < k && arr[i].a == 1) {
					if(arr[i].b == 1) {
						q1.addFirst(arr[i]);
						if(q2.size() == k) {
							ans -= q2.removeLast().t;
						}
						flag = 1;
					}
					else
						q1.addLast(arr[i]);
					ans += arr[i].t;
				}
				if(q2.size() < k && arr[i].b == 1) {
					if(arr[i].a == 1) {
						q2.addFirst(arr[i]);
						if(flag == 0 && q1.size() == k) {
							ans -= q1.removeLast().t;
						}
					}
					else
						q2.addLast(arr[i]);
					if(flag == 0)
						ans += arr[i].t;
				}
				
			}
			
//			if(q1.size() < s) {
//				int a = s-q1.size();
//				while(a>0) {
//					ans -= q2.removeLast().t;
//					a--;
//				}
//			}
//			else if(q2.size() < s) {
//				int a = s-q2.size();
//				while(a>0) {
//					ans -= q1.removeLast().t;
//					a--;
//				}
//			}
//			System.out.println(s);
//			System.out.println(q1.size());
//			System.out.println(q2.size());
			System.out.println(ans);
			
		}
		
		out.flush();
	}
	
  
	public static boolean check(int mid, int c, int[] arr, int k) {
		int ans = 0;
		for(int i=0; i<arr.length; i++) {
			if(c==0) {
				ans++;
				c = (c+1)%2;
			}else {
				if(arr[i]<=mid) {
					ans++;
					c = (c+1)%2;
				}
			}
		}
		return ans>=k;
	}
	public static int power(int x, int y, int p) 
    { 
        // Initialize result 
        int res = 1;      
         
        // Update x if it is more   
        // than or equal to p 
        x = x % p;  
  
       if (x == 0) return 0; // In case x is divisible by p;    
  
        while (y > 0) 
        { 
            // If y is odd, multiply x 
            // with result 
            if((y & 1)==1) 
                res = (res * x) % p; 
      
            // y must be even now 
            // y = y / 2 
            y = y >> 1;  
            x = (x * x) % p;  
        } 
        return res; 
    } 
	
	public static class Pair implements Comparable<Pair>{
		int i;
		int t;
		int a;
		int b;
		Pair(int i,int m, int s, int k){
			this.i = i;
			t = m;
			a = s;
			b = k;
		}
		public int compareTo(Pair p) {
			return this.t-p.t;
		}
	}
    
}

