import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.Point;
 
public class Main {
	//static final long MOD = 998244353L;
	//static final long INF = 1000000000000000007L;
	static String letters = "abcdefghijklmnopqrstuvwxyz";
	//static final long MOD = 1000000007L;
	static final int INF = 1000000007;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		int M = sc.ni();
		int K = sc.ni();
		int X = 0;
		int[][] input = new int[N][3];

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 3; j++)
				input[i][j] = sc.ni();

			if (2*input[i][1]+input[i][2]==3)
				X++;
		}

		int[][] both = new int[X][2];
		int i1 = 0;
		int[][] nums = new int[N-X][4]; //val,type (0 is neither, 1 is bob, 2 is alice),index in nums, index in input
		int i2 = 0;
		for (int i = 0; i < N; i++) {
			int[] in = input[i];
			if (2*in[1]+in[2]==3) {
				both[i1][0] = in[0];
				both[i1][1] = i;
				i1++;
			} else {
				nums[i2][0] = in[0];
				nums[i2][1] = 2*in[1]+in[2];
				nums[i2][2] = i2;
				nums[i2][3] = i;
				i2++;
			}
		}
		both = sort(both);
		int[] bothPref = new int[X+1];
		for (int i = 1; i <= X; i++)
			bothPref[i] = bothPref[i-1]+both[i-1][0];
		
		nums = sort(nums);
		
		
		ArrayList<Integer> alice = new ArrayList<Integer>();
		ArrayList<Integer> bob = new ArrayList<Integer>();
		for (int i = 0; i < N-X; i++) {
			int[] in = nums[i];
			if (in[1]==1)
				bob.add(i);
			else if (in[1]==2)
				alice.add(i);
		}
		
		BinaryIndexedTree exists = new BinaryIndexedTree(N-X);
		BinaryIndexedTree bit = new BinaryIndexedTree(N-X);
		for (int i = 0; i < N-X; i++) {
			exists.add(1, i);
			bit.add(nums[i][0],i);
		}
		
		int ans = Integer.MAX_VALUE;
		int readBoth = -1;
		for (int i = M; i >= K; i--) {
			//i is the number of books read that are liked by both
			if (i < bothPref.length) {
				int rem = M-i;
				if (rem <= N-X) {
					int total = bothPref[i]+bit.sum(rem);
					if (total < ans) {
						ans = total;
						readBoth = i;
					}
				}
			}
		}
		
		int taken = 0;
		int read = 0;
		for (int i = K-1; i >= 0; i--) {
			if (2*K-i > M) break;
			if (K-1-i < bob.size() && K-1-i < alice.size()) {
				int b = nums[bob.get(K-1-i)][0];
				int a = nums[alice.get(K-1-i)][0];
				exists.add(-1, bob.get(K-1-i));
				exists.add(-1, alice.get(K-1-i));
				bit.add(0-b,bob.get(K-1-i));
				bit.add(0-a,alice.get(K-1-i));
				taken += b;
				taken += a;
				read += 2;
			} else {
				break;
			}

			//i is the number of books read that are liked by both
			if (i < bothPref.length) {
				int rem = M-i-read;
				if (rem <= exists.sum(N-X)) {
					int low = 0;
					int high = N-X;
					while (low < high) {
						int med = (low+high)/2;
						if (bit.sum(med) < rem) {
							low = med+1;
						} else {
							high = med;
						}
					}
					
					int total = bothPref[i]+bit.sum(low)+taken;
					if (total < ans) {
						ans = total;
						readBoth = i;
					}
				}
			}
			
		}
		
		if (ans == Integer.MAX_VALUE) {
			pw.println(-1);
		} else {
			ArrayList<Integer> take = new ArrayList<Integer>();
			for (int i = 0; i < readBoth; i++) {
				take.add(1+both[i][1]);
			}
			
			for (int i = 0; i < K-readBoth; i++) {
				take.add(1+nums[alice.get(i)][3]);
				take.add(1+nums[bob.get(i)][3]);
			}
			
			PriorityQueue<Pair> left = new PriorityQueue<Pair>();
			for (int[] in: nums) {
				if (in[1] == 0)
					left.add(new Pair(in[0],in[3]));
			}
			if (K-readBoth < alice.size()) {
				for (int i = Math.max(0,K-readBoth); i < alice.size(); i++) {
					left.add(new Pair(nums[alice.get(i)][0],nums[alice.get(i)][3]));
				}
			}
			if (K-readBoth < bob.size()) {
				for (int i = Math.max(0,K-readBoth); i < bob.size(); i++) {
					left.add(new Pair(nums[bob.get(i)][0],nums[bob.get(i)][3]));
				}
			}

			while (!left.isEmpty() && take.size() < M) {
				Pair p = left.poll();
				take.add(1+p.index);
			}
			Collections.sort(take);
			
			pw.println(ans);
			for (int t: take)
				pw.print(t + " ");
			
			if (ans==8195)
				pw.println(readBoth);
		}
		pw.close();
	}
	
	static class Pair implements Comparable<Pair> {
		public int val;
		public int index;
		
		public Pair(int val, int index) {
			this.val = val;
			this.index = index;
		}
		
		public int compareTo(Pair p) {
			return val-p.val;
		}
	}
	
	static class BinaryIndexedTree {
		public int[] arr;

		public BinaryIndexedTree (int N) {
			arr = new int[N+1];
		}

		//add k to the i-th element.
		public void add(int k, int i) {
			int node = i+1;
			while (node < arr.length) {
				arr[node] += k;
				node += node & (-node);
			}
		}

		//sum up the elements from input[s_i] to input[e_i], from [s_i,e_i).
		public int sum(int s_i, int e_i) {
			return sum(e_i) - sum(s_i);
		}

		public int sum(int i) {
			int total = 0;
			int node = i;
			while (node > 0) {
				total += arr[node];
				node -= node & (-node);
			}
			return total;
		}
	}
	
	public static long dist(long[] p1, long[] p2) {
		return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1]));
	}
	
	//Find the GCD of two numbers
	public static long gcd(long a, long b) {
		if (a < b) return gcd(b,a);
		if (b == 0)
			return a;
		else
			return gcd(b,a%b);
	}
	
	//Fast exponentiation (x^y mod m)
	public static long power(long x, long y, long m) { 
		if (y < 0) return 0L;
		long ans = 1;
		x %= m;
		while (y > 0) { 
			if(y % 2 == 1) 
				ans = (ans * x) % m; 
			y /= 2;  
			x = (x * x) % m;
		} 
		return ans; 
	}
	
	public static int[] shuffle(int[] array) {
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    int temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		return array;
	}
	
	public static long[] shuffle(long[] array) {
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    long temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		return array;
	}
	
    public static int[][] sort(int[][] array) {
    	//Sort an array (immune to quicksort TLE)
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    int[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		Arrays.sort(array, new Comparator<int[]>() {
			  @Override
        	  public int compare(int[] a, int[] b) {
				  return a[0]-b[0]; //ascending order
	          }
		});
		return array;
	}
    
    public static long[][] sort(long[][] array) {
    	//Sort an array (immune to quicksort TLE)
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    long[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		Arrays.sort(array, new Comparator<long[]>() {
			  @Override
        	  public int compare(long[] a, long[] b) {
				  if (a[0] < b[0])
					  return -1;
				  else
					  return 1;
	          }
		});
		return array;
	}
    
    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            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 ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
}