import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public final class ReadingBooksHard {

    private static final FastReader fr = new FastReader();

    public static void main(String[] args) {
        final int n = fr.nextInt(), m = fr.nextInt(), k = fr.nextInt();
        final Book[] books = new Book[n];
        for (int i = 0; i < n; i++) {
            books[i] = new Book(i, fr.nextInt(), fr.nextInt() == 1, fr.nextInt() == 1);
        }
        final Book[] ans = new ReadingBooksHard(books, m, k).solve();

        System.out.println(ans == null ? -1 : Arrays.stream(ans).mapToInt(b -> b.time).sum());
        if(ans != null) {
            final StringBuilder sb = new StringBuilder();
            for (Book b : ans) {
                sb.append(b.index + 1).append(' ');
            }
            System.out.println(sb);
        }
    }

    private final Book[] books;
    private final int m, k;

    public ReadingBooksHard(Book[] books, int m, int k) {
        this.books = books;
        this.m = m;
        this.k = k;
    }

    public Book[] solve() {
        final Map<Interest, List<Book>> map = Arrays.stream(books).collect(Collectors.groupingBy(b -> b.interest));
        for (Interest interest : Interest.values()) {
            List<Book> books = map.get(interest);
            if(books == null) map.put(interest, Collections.emptyList());
            else books.sort(null);
        }

        final Book[] template = new Book[0];
        final Book[] alices = map.get(Interest.ALICE).toArray(template),
                bobs = map.get(Interest.BOB).toArray(template),
                commons = map.get(Interest.COMMON).toArray(template),
                nons = map.get(Interest.NON).toArray(template);
        int ai = 0, bi = 0, ci = 0, ni = 0;

        int bestTimeForK = Integer.MAX_VALUE;
        List<Book> bestForK = Collections.emptyList();
        for (int commonCount = k; commonCount >= 0; commonCount--) {
            int aai = 0, bbi = 0, cci = 0;
            if (commons.length < commonCount || alices.length < k - commonCount || bobs.length < k - commonCount)
                continue;

            final List<Book> chosen = new ArrayList<>(commonCount + 2 * (k - commonCount));
            for (int i = 0; i < commonCount; i++) {
                chosen.add(commons[cci++]);
            }
            for (int i = 0; i < k - commonCount; i++) {
                chosen.add(alices[aai++]);
                chosen.add(bobs[bbi++]);
            }

            final int time = chosen.stream().mapToInt(b -> b.time).sum();
            if (time < bestTimeForK) {
                bestTimeForK = time;
                bestForK = chosen;
                ai = aai;
                bi = bbi;
                ci = cci;
            }
        }

        final List<Book> books = new ArrayList<>(m + k);
        books.addAll(bestForK);

        while (books.size() != m) {
            if (books.size() < m) { // Common to separates or add non
                Integer commonToSeparateChange, addNonChange;

                if (ci == 0 || ai == alices.length || bi == bobs.length) commonToSeparateChange = null;
                else {
                    Book common = commons[ci - 1], alice = alices[ai], bob = bobs[bi];
                    commonToSeparateChange = alice.time + bob.time - common.time;
                }

                if (ni == nons.length) addNonChange = null;
                else {
                    Book non = nons[ni];
                    addNonChange = non.time;
                }

                if (commonToSeparateChange != null && addNonChange != null) {
                    if(commonToSeparateChange < addNonChange) {
                        books.remove(commons[--ci]);
                        books.add(alices[ai++]);
                        books.add(bobs[bi++]);
                    } else {
                        books.add(nons[ni++]);
                    }
                } else if (commonToSeparateChange != null) {
                    books.remove(commons[--ci]);
                    books.add(alices[ai++]);
                    books.add(bobs[bi++]);
                } else if (addNonChange != null) {
                    books.add(nons[ni++]);
                } else return null;

            } else { // Separates to common
                if (ai == 0 || bi == 0 || ci == commons.length) return null;
                books.remove(alices[--ai]);
                books.remove(bobs[--bi]);
                books.add(commons[ci++]);
            }
        }

        return books.toArray(template);

    }

    private enum Interest {
        COMMON, ALICE, BOB, NON;
    }

    private static final class Book implements Comparable<Book> {
        public final int time, index;
        public final Interest interest;

        public Book(final int index, int time, boolean alice, boolean bob) {
            this.index = index;
            this.time = time;
            if (alice && bob) interest = Interest.COMMON;
            else if (alice) interest = Interest.ALICE;
            else if (bob) interest = Interest.BOB;
            else interest = Interest.NON;
        }

        @Override
        public int compareTo(Book o) {
            return Integer.compare(this.time, o.time);
        }
    }

    private static final class FastReader {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

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

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

}