// Main Code at the Bottom
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger; 
public class Main {
	//Fast IO class
    static class FastReader {
        BufferedReader br; 
        StringTokenizer st; 
        public FastReader() {
        	boolean env=System.getProperty("ONLINE_JUDGE") != null;
        	if(!env) {
        		try {
					br=new BufferedReader(new FileReader("src\\input.txt"));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
        	}
        	else 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; 
        } 
    }     
    static long MOD=1000000000+7;
    //debug
    static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
    // Pair
    static class pair{
    	long x,y;
    	pair(long a,long b){
    		this.x=a;
    		this.y=b;
    	}
    	public boolean equals(Object obj) {
    		if(obj == null || obj.getClass()!= this.getClass()) return false;
            pair p = (pair) obj;
            return (this.x==p.x && this.y==p.y);
        }
    	public int hashCode() {
            return Objects.hash(x,y);
        }
    }
    static FastReader sc=new FastReader();
    static PrintWriter out=new PrintWriter(System.out);  
    //Main function(The main code starts from here)
    static class obj{
    	long t,a,b;
    	obj(long x,long y,long z){
    		t=x;
    		a=y;
    		b=z;
    	}
    }
    static int n,m,k;
    static obj books[];
    static Comparator<Integer> cmp=new Comparator<Integer>() {
    	public int compare(Integer i1,Integer i2) {
    		if(books[i1].t==books[i2].t) return i1-i2;
    		return (int)(books[i1].t-books[i2].t);
    	}
    };
    public static void main (String[] args) throws java.lang.Exception {
    	int test=1;
    	//test=sc.nextInt();
    	while(test-->0){
    		n=sc.nextInt();m=sc.nextInt();k=sc.nextInt();
    		books=new obj[n];
    		for(int i=0;i<n;i++) books[i]=new obj(sc.nextLong(),sc.nextLong(),sc.nextLong());
    		TreeSet<Integer> s1=new TreeSet<>(cmp),s2=new TreeSet<>(cmp),s3=new TreeSet<>(cmp),s4=new TreeSet<>(cmp);
    		for(int i=0;i<n;i++) {
    			if(books[i].a==1 && books[i].b==1) s1.add(i);
    			else if(books[i].a==1 && books[i].b==0) s2.add(i);
    			else if(books[i].a==0 && books[i].b==1) s3.add(i);
    			else s4.add(i);
    		}
    		long ans=0;
    		TreeSet<Integer> a1=new TreeSet<>(cmp),a2=new TreeSet<>(cmp),a3=new TreeSet<>(cmp);
    		for(int i=0;i<k;i++) {
    			if(s1.isEmpty() && (s2.isEmpty() || s3.isEmpty())) {
    				ans=-1;
    				break;
    			}
    			long v1=Long.MAX_VALUE,v2=Long.MAX_VALUE;
    			if(!s1.isEmpty()) v1=books[s1.first()].t;
    			if(!s2.isEmpty() && !s3.isEmpty()) v2=books[s2.first()].t+books[s3.first()].t;
    			if(v1<=v2) {
    				ans+=v1;
    				a1.add(s1.pollFirst());
    			}
    			else {
    				ans+=v2;
    				a2.add(s2.pollFirst());
    				a3.add(s3.pollFirst());
    			}
    		}
    		int len=a1.size()+a2.size()+a3.size();
    		TreeSet<Integer> tmp=new TreeSet<>(cmp);
    		if(len<=m) {
    			while(len<m) {
    				if(s1.isEmpty() && s2.isEmpty() && s3.isEmpty() && s4.isEmpty()) break;
    				long v1=Long.MAX_VALUE,v2=Long.MAX_VALUE,v3=Long.MAX_VALUE,v4=Long.MAX_VALUE;
    				if(!s1.isEmpty()) v1=books[s1.first()].t;
    				if(!s2.isEmpty()) v2=books[s2.first()].t;
    				if(!s3.isEmpty()) v3=books[s3.first()].t;
    				if(!s4.isEmpty()) v4=books[s4.first()].t;
    				if(v1<v2 && v1<v3 && v1<v4) {
    					ans+=v1;
    					tmp.add(s1.pollFirst());
    				}
    				else if(v2<v3 && v3<v1 && v2<v4) {
    					ans+=v2;
    					tmp.add(s2.pollFirst());
    				}
    				else if(v3<v1 && v3<v2 && v3<v4){
    					ans+=v3;
    					tmp.add(s3.pollFirst());
    				}
    				else {
    					ans+=v4;
    					tmp.add(s4.pollFirst());
    				}
    				len++;
    			}
    			if(len!=m) {
    				out.println(-1);
    				continue;
    			}
    		}
    		else {
    			if(len-a2.size()>m) {
    				out.println(-1);
    				continue;
    			}
    			while(len>m) {
    				if(s1.isEmpty()) break;
    				int x1=a2.pollLast(),x2=a3.pollLast(),x3=s1.pollFirst();
    				ans=ans+books[x3].t-books[x1].t-books[x2].t;
    				a1.add(x3);
    				len--;
    			}
    			if(len!=m) {
    				out.println(-1);
    				continue;
    			}
    		}
    		out.println(ans);
    		for(int x: a1) out.print((x+1)+" ");
    		for(int x: a2) out.print((x+1)+" ");
    		for(int x: a3) out.print((x+1)+" ");
    		for(int x: tmp) out.print((x+1)+" ");
    	}
        out.flush();
        out.close();
    }
}