import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        PriorityQueue<Pair>a = new PriorityQueue<>();
        PriorityQueue<Pair>b = new PriorityQueue<>();
        PriorityQueue<Pair>both = new PriorityQueue<>();
        PriorityQueue<Pair>garb = new PriorityQueue<>();
        for(int i=0;i<n;i++){
            int t = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x==1 && y==1){
                both.add(new Pair(t,i+1));
            }else if(x==1 && y!=1){
                a.add(new Pair(t,i+1));
            }else if(x!=1 && y==1){
                b.add(new Pair(t,i+1));
            }else{
                garb.add(new Pair(t,i+1));
            }
        }
//        System.out.println(a);
//        System.out.println(b);
//        System.out.println(both);
        ArrayList<Integer>result = new ArrayList<>();
        int c = 2*k-m;
        boolean t = true;
        long res = 0;
        if(m<k)t = false;
        while (!both.isEmpty() && c>0 && t){
            if(both.isEmpty()){
                t = false;break;}
            Pair p = both.poll();
            res+=p.a;
            result.add(p.b);
            c--;
            k--;
            m--;
        }
        while (k>0 && t){
            int x = 1000000000;
            int y = 1000000000;
            int z = 1000000000;
            if(!a.isEmpty()){

                x=a.peek().a;
            }
            if(!b.isEmpty())y = b.peek().a;
            if(!both.isEmpty())z = both.peek().a;
            if(z<=x+y && !both.isEmpty()){
                Pair p = both.poll();
                res+=p.a;
                result.add(p.b);
                m--;
            }
            else if(!a.isEmpty() && !b.isEmpty()){
                Pair p1 = a.poll();
                Pair p2 = b.poll();
                res+=p1.a;
                res+=p2.a;
                result.add(p1.b);
                result.add(p2.b);
                m-=2;
            }else{
                t = false;
                break;
            }
            k--;
        }
        while (m>0){
            int x = 1000000000;
            int y = 1000000000;
            int z = 1000000000;
            int g = 1000000000;
            if(!a.isEmpty()){

                x=a.peek().a;
            }
            if(!b.isEmpty())y = b.peek().a;
            if(!both.isEmpty())z = both.peek().a;
            if(!garb.isEmpty())g = garb.peek().a;
            if(z<=x&& z<=y && z<=g&& !both.isEmpty()){
                Pair p = both.poll();
                res+=p.a;
                result.add(p.b);
                m--;
            }
            else if(x<=y && x<=z && x<=g &&!a.isEmpty()){
                Pair p1 = a.poll();
                res+=p1.a;
                result.add(p1.b);
                m--;
            }else if(y<=z && y<=x && y<=g&&!b.isEmpty()){
                Pair p1 = b.poll();
                res+=p1.a;
                result.add(p1.b);
                m--;
            }else if(g<=z && g<=x && g<=y&&!garb.isEmpty()){
                Pair p1 = garb.poll();
                res+=p1.a;
                result.add(p1.b);
                m--;
            }else{
                t = false;
                break;
            }
        }
        if(t){
            pw.println(res);
            for(int i=0;i<result.size();i++)
                pw.print(result.get(i)+" ");
            pw.println();
        }else{
            pw.println(-1);
        }
        pw.flush();
        pw.close();
    }
    static long power(long x, long y, long m) {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        if (y % 2 == 0)
            return p;
        else
            return (x * p) % m;
    }
    static class Node{
        long a;
        long b;
        long c;
        public Node(long a,long b,long c){
            this.a= a;
            this.b = b;
            this.c = c;
        }
    }
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }
    static class Pair implements Comparable<Pair>{
        int a;
        int b;
        public Pair(int a,int b){
            this.a= a;
            this.b = b;
        }

        public int compareTo(Pair o) {
            if(this.a==o.a)return Integer.compare(b,o.b);
            return Integer.compare(a,o.a);
        }
    }

}