//Created by Aminul on 6/28/2020.

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class E2_2 {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
        Pair alice[] = new Pair[n + 1];
        Pair bob[] = new Pair[n + 1];
        Pair both[] = new Pair[n + 1];

        Pair arr[] = new Pair[n];

        int x = 1, y = 1, z = 1;
        for (int i = 1; i <= n; i++) {
            int t = in.nextInt(), a = in.nextInt(), b = in.nextInt();
            if (a == 1 && b == 1) {
                both[z++] = new Pair(t, i);
            } else if (a == 1) {
                alice[x++] = new Pair(t, i);
            } else if (b == 1) {
                bob[y++] = new Pair(t, i);
            }
            arr[i - 1] = new Pair(t, i);
        }


        alice = prepArray(alice, x);
        bob = prepArray(bob, y);
        both = prepArray(both, z);

        long res = Long.MAX_VALUE;
        int common = -1;

        for (int i = 0; i < both.length; i++) {
            if (k - i < alice.length && k - i < bob.length &&
                    k - i >= 0 && (k - i) + (k - i) + i <= m) {
                long curr = both[i].sum + alice[k - i].sum + bob[k - i].sum;
                if (curr < res) {
                    res = curr;
                    common = i;
                }
            }
        }

        if (res == Long.MAX_VALUE) {
            pw.println(-1);
        } else {
            TreeSet<Integer> set = new TreeSet<>();
            for (int i = 1; i <= common; i++) {
                set.add(both[i].idx);
            }
            for (int i = 1; i <= k - common; i++) {
                set.add(alice[i].idx);
                set.add(bob[i].idx);
            }

            Arrays.sort(arr);
            for (Pair p : arr) {
                if (set.size() >= m) break;
                if (set.add(p.idx)) {
                    res += p.sum;
                }
            }

            if (set.size() < m) {
                pw.println(-1);
                throw new RuntimeException("ERROR!");
            } else {
                pw.println(res);
                for (int i : set) pw.print(i + " ");
                pw.println();
            }

        }

        pw.close();
    }

    static Pair[] prepArray(Pair arr[], int len) {
        arr = Arrays.copyOf(arr, len);
        arr[0] = new Pair(0, 0);
        Arrays.sort(arr);
        for (int i = 1; i < arr.length; i++) {
            arr[i].sum += arr[i - 1].sum;
        }
        return arr;
    }

    static class Pair implements Comparable<Pair> {
        long sum;
        int idx;

        public Pair(long sum, int idx) {
            this.sum = sum;
            this.idx = idx;
        }

        @Override
        public int compareTo(Pair o) {
            return Long.compare(sum, o.sum);
        }

    }

    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }
}