import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;

public class E {
    //Solution by Sathvik Kuthuru
    public static void main(String[] args) {
        FastReader scan = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        Task solver = new Task();
        int t = 1;
        for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
        out.close();
    }

    static class Task {

        public void solve(int testNumber, FastReader scan, PrintWriter out) {
            int n = scan.nextInt(), m = scan.nextInt(), k = scan.nextInt();
            ArrayList<Item> both1 = new ArrayList<>(), first = new ArrayList<>(), second = new ArrayList<>();
            ArrayList<Item> both2 = new ArrayList<>();
            ArrayList<Item> left = new ArrayList<>();
            for(int i = 0; i < n; i++) {
                int t = scan.nextInt(), a = scan.nextInt(), b = scan.nextInt();
                Item curr = new Item(t);
                curr.index.add(i + 1);
                if(a == 1 && b == 1) both1.add(curr);
                else if(a == 1) first.add(curr);
                else if(b == 1) second.add(curr);
                else left.add(curr);
            }
            int min = Math.min(first.size(), second.size());
            Collections.sort(first);
            Collections.sort(second);
            for(int i = 0; i < min; i++) {
                Item now = new Item(first.get(i).val + second.get(i).val);
                now.index.addAll(first.get(i).index);
                now.index.addAll(second.get(i).index);
                both2.add(now);
            }
            for(int i = min + 1; i < first.size(); i++) left.add(first.get(i));
            for(int i = min + 1; i < second.size(); i++) left.add(second.get(i));
            Collections.sort(both1);
            Collections.sort(both2);
            Collections.sort(left);
            long ans = 0, liked = 0, have = 0;
            ArrayList<Item> res = new ArrayList<>();
            int firstIndex = 0, secondIndex = 0;
            while(true) {
                if(liked == k || have == m) break;
                if(firstIndex == both1.size() && secondIndex == both2.size()) break;
                if(m - have == 1) {
                    if (firstIndex == both1.size()) {
                        break;
                    }
                    else {
                        have++;
                        liked++;
                        ans += both1.get(firstIndex).val;
                        res.add(both1.get(firstIndex));
                        firstIndex++;
                    }
                }
                else {
                    if (firstIndex == both1.size()) {
                        have += 2;
                        liked++;
                        ans += both2.get(secondIndex).val;
                        res.add(both2.get(secondIndex));
                        secondIndex++;

                    }
                    else if (secondIndex == both2.size()) {
                        have++;
                        liked++;
                        ans += both1.get(firstIndex).val;
                        res.add(both1.get(firstIndex));
                        firstIndex++;
                    }
                    else {
                        int firstVal = both1.get(firstIndex).val;
                        int secondVal = both2.get(secondIndex).val;
                        if(firstVal <= secondVal) {
                            have++;
                            liked++;
                            ans += both1.get(firstIndex).val;
                            res.add(both1.get(firstIndex));
                            firstIndex++;
                        }
                        else {
                            have += 2;
                            liked++;
                            ans += both2.get(secondIndex).val;
                            res.add(both2.get(secondIndex));
                            secondIndex++;
                        }
                    }
                }
            }
            if(have < m && liked < k) {
                out.println(-1);
                return;
            }
            if(liked < k) {
                int canChange = Math.min(both1.size() - firstIndex, secondIndex);
                if(canChange < k - liked) {
                    out.println(-1);
                    return;
                }
                for(int i = 0; i < k - liked; i++) {
                    int currFirst = firstIndex + i;
                    int currSecond = secondIndex - i - 1;
                    both2.get(currSecond).used = false;
                    res.add(both1.get(currFirst));
                    ans -= both2.get(currSecond).val;
                    ans += both1.get(currFirst).val;
                }
            }
            else if(have < m) {
                if(have + left.size() < m) {
                    out.println(-1);
                    return;
                }
                for(int i = 0; i < m - have; i++) {
                    ans += left.get(i).val;
                    res.add(left.get(i));
                }
            }
            out.println(ans);
            for(Item x : res) {
                if(x.used) {
                    for(int j : x.index) out.printf("%d ", j);
                }
            }
        }

        static class Item implements Comparable<Item> {
            int val;
            ArrayList<Integer> index;
            boolean used;

            public Item(int a) {
                val = a;
                index = new ArrayList<>();
                used = true;
            }

            @Override
            public int compareTo(Item item) {
                return Integer.compare(val, item.val);
            }
        }

    }

    static void shuffle(int[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            int temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }

    static void shuffle(long[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            long temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        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;
        }
    }

}