import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class ReadingBooksHard {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();

        ArrayList<Integer[]> aliceBob = new ArrayList<>();
        ArrayList<Integer[]> alice = new ArrayList<>();
        ArrayList<Integer[]> bob = new ArrayList<>();
        ArrayList<Integer[]> none = new ArrayList<>();

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

            if (a && b) aliceBob.add(new Integer[] {time, i + 1});
            else if (a) alice.add(new Integer[] {time, i + 1});
            else if (b) bob.add(new Integer[] {time, i + 1});
            else none.add(new Integer[] {time, i + 1});
        }

        if (aliceBob.size() + Math.min(alice.size(), bob.size()) < k) {
            System.out.println("-1");
            return;
        }

        BookComparator comparator = new BookComparator();
        aliceBob.sort(comparator);
        alice.sort(comparator);
        bob.sort(comparator);
        none.sort(comparator);

        aliceBob.add(new Integer[] {Integer.MAX_VALUE, -1});
        alice.add(new Integer[] {Integer.MAX_VALUE, -1});
        bob.add(new Integer[] {Integer.MAX_VALUE, -1});
        none.add(new Integer[] {Integer.MAX_VALUE, -1});

        ArrayList<Integer[]> readBooks = new ArrayList<>();

        int abPtr = 0;
        int aPtr = 0;
        int bPtr = 0;
        int nPtr = 0;

        int fixedSelections = 2 * k - m;
        int aliceBooks, bobBooks;

        if (fixedSelections > 0) {
            if (aliceBob.size() - 1 < fixedSelections) {
                System.out.println("-1");
                return;
            }
            // Collecting fixed books
            for (int i = 0; i < fixedSelections; i++) {
                readBooks.add(aliceBob.get(abPtr++));
            }
            aliceBooks = k - fixedSelections;
            bobBooks = k - fixedSelections;
        } else {
            aliceBooks = bobBooks = k;
        }

        // Alice collecting books
        for (int i = 0; i < aliceBooks; i++) {
            if (isLess(aliceBob.get(abPtr), alice.get(aPtr))) {
                readBooks.add(aliceBob.get(abPtr++));
                bobBooks--;
            } else {
                readBooks.add(alice.get(aPtr++));
            }
        }

        // Bob collecting books
        for (int i = 0; i < bobBooks; i++) {
            if (isLess(aliceBob.get(abPtr), bob.get(bPtr))) {
                readBooks.add(aliceBob.get(abPtr++));
            } else {
                readBooks.add(bob.get(bPtr++));
            }
        }

        while (readBooks.size() < m) {
            if (isLess(aliceBob.get(abPtr), alice.get(aPtr), bob.get(bPtr), none.get(nPtr))) {
                readBooks.add(aliceBob.get(abPtr++));
            } else if (isLess(alice.get(aPtr), bob.get(bPtr), none.get(nPtr))) {
                readBooks.add(alice.get(aPtr++));
            } else if (isLess(bob.get(bPtr), none.get(nPtr))) {
                readBooks.add(bob.get(bPtr++));
            } else readBooks.add(none.get(nPtr++));
        }

        int minTime = 0;
        for (int i = 0; i < m; i++) {
            minTime += readBooks.get(i)[0];
        }
        System.out.println(minTime);
        for (int i = 0; i < m; i++) {
            System.out.print(readBooks.get(i)[1] + ((i == m - 1) ? "\n" : " "));
        }
    }

    private static boolean isLess(Integer[] book1, Integer[] book2) {
        return book1[0] < book2[0];
    }

    private static boolean isLess(Integer[] book1, Integer[] book2, Integer[] book3) {
        return book1[0] < book2[0] && book1[0] < book3[0];
    }

    private static boolean isLess(Integer[] book1, Integer[] book2, Integer[] book3, Integer[] book4) {
        return book1[0] < book2[0] && book1[0] < book3[0] && book1[0] < book4[0];
    }

    private static class BookComparator implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            Integer[] x1 = (Integer[]) o1;
            Integer[] x2 = (Integer[]) o2;
            return x1[0].compareTo(x2[0]);
        }
    }
}