import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main {
    static int updateSt(TreeSet<pair> st, TreeSet<pair> fr, int sum, int need){
                need = Math.max(need, 0);
                boolean useful = false;
                while(true){
                    useful = false;
                    while(st.size() > need){
                        pair temp = st.last();
                        sum -= temp.time;
                        fr.add(temp);
                        st.remove(temp);
                        useful = true;
                    }

                    while(st.size() < need && fr.size() > 0){
                        pair temp = fr.first();
                        sum += temp.time;
                        fr.remove(temp);
                        st.add(temp);
                        useful = true;
                    }

                    while(st.size() > 0 && fr.size() > 0 && st.last().time > fr.first().time){
                        pair f = fr.first();
                        pair s = st.last();
                        sum += f.time;
                        sum -= s.time;

                        fr.remove(f);
                        fr.add(s);
                        st.remove(s);
                        st.add(f);
                        useful = true;
                    }
                    if(!useful)
                        break;
                }

                return sum;
            }


	public static void main(String args[])
	{
		Scanner sc= new Scanner(System.in);


   		//Your code goes here...
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        ArrayList<pair>[] times = new ArrayList[4];
        for (int i=0; i<4; i++) {
            times[i] = new ArrayList<>();
        }

        for (int i=0; i<n; i++) {
            int t = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();

            times[2*a + b].add(new pair(t, i));
        }

        ArrayList<Integer>[] sums = new ArrayList[4];

        for (int i=0; i<4; i++) {
            Collections.sort(times[i]);
            sums[i] = new ArrayList<>();
            sums[i].add(0);
            for(pair it:times[i])
                sums[i].add(sums[i].get(sums[i].size()-1) + it.time);
        }

        int ans = Integer.MAX_VALUE;
        int pos = Integer.MAX_VALUE;
        ArrayList<pair> res = new ArrayList<>();
        int sum = 0;
        TreeSet<pair> st = new TreeSet<>();
        TreeSet<pair> fr = new TreeSet<>();
        int start = 0;

        for(int iter = 0; iter<2; iter++){
            sum = 0;
            fr.clear();
            st.clear();
            start = 0;

            while((k-start)>=sums[1].size() || (k-start)>=sums[2].size() || m - start - (k-start)*2<0)
                start ++;

            if(start>=sums[3].size()){
                System.out.println("-1");
                return;
            }

            int need = m - start - (k-start)*2;

            for (int i=0; i<3; i++) {
                for (int p=times[i].size()-1; p>=(i==0?0:k-start); p--) {
                    fr.add(times[i].get(p));
                }
            }

            sum = updateSt(st, fr, sum, need);

            for (int cnt=start; cnt<(iter==0?sums[3].size():pos); cnt++) {
                if(k-cnt>=0){
                    if(cnt + (k-cnt)*2 + st.size() == m){
                        if(sums[3].get(cnt) + sums[1].get(k-cnt) + sums[2].get(k-cnt) + sum < ans){
                            ans = sums[3].get(cnt) + sums[1].get(k-cnt) + sums[2].get(k-cnt) + sum;
                            pos = cnt + 1;
                        }
                    }
                }
                else{
                    if(cnt + st.size() == m){
                        if(sum + sums[3].get(cnt) < ans){
                            ans = sum + sums[3].get(cnt);
                            pos = cnt + 1;
                        }
                    }
                }

                if(iter == 1 && cnt + 1 == pos)
                    break;

                need -= 1;
                if(k-cnt>0){
                    need += 2;
                    fr.add(times[1].get(k-cnt-1));
                    fr.add(times[2].get(k-cnt-1));
                }
                sum = updateSt(st, fr, sum, need);
            }

            if(iter == 1){
                for (int i=0; i+1<pos; i++) {
                    res.add(times[3].get(i));
                }
                for (int i=0; i<=k-pos; i++) {
                    res.add(times[1].get(i));
                    res.add(times[2].get(i));
                }
                for (pair ss: st) {
                    res.add(ss);
                }
            }
        }

        System.out.println(ans);
        for (int i=0; i<res.size(); i++) {
            System.out.print((res.get(i).pos+1)+" ");
        }

}

}

class pair implements Comparable<pair>{
            int time, pos;
            pair(int time, int pos){
                this.time = time;
                this.pos = pos;
            }

            public int compareTo(pair a) {
                int comparetime=((pair)a).time;
                /* For Ascending order*/
                return this.time-comparetime;
            }
}