import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.AbstractCollection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Hello
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E2ReadingBooksHardVersion {
        public void solve(int testNumber, InputReader c, OutputWriter w) {
            int n = c.readInt(), m = c.readInt(), k = c.readInt();
            ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(), b = new ArrayList<>();
            PriorityQueue<Pair<Integer, Pair<Integer, Integer>>> tot = new PriorityQueue<>();
            ArrayList<Pair<Integer, Pair<Integer, Integer>>> whoCare = new ArrayList<>();
            HashSet<Integer> resInd = new HashSet<>();
            int cst[] = new int[n];
            for (int i = 0; i < n; i++) {
                int t = c.readInt(), aa = c.readInt(), bb = c.readInt();
                cst[i] = t;
                if (aa == 0 || bb == 0) {
                    if (aa == 1) {
                        a.add(new Pair<>(t, i));
                    } else if (bb == 1) {
                        b.add(new Pair<>(t, i));
                    } else {
                        whoCare.add(new Pair<>(t, new Pair<>(i, 0)));
                    }
                } else {
                    tot.add(new Pair<>(t, new Pair<>(0, i)));
                }
            }
            Collections.sort(a);
            Collections.sort(b);

            HashMap<Integer, Pair<Pair<Integer, Integer>, Pair<Integer, Integer>>> kk = new HashMap<>();
            int id = 1;
            for (int i = 0; i < a.size() && i < b.size(); i++) {
                Pair<Integer, Integer> a1 = a.get(i), b1 = b.get(i);

                tot.add(new Pair<>(a1.first + b1.first, new Pair<>(id, -1)));
                kk.put(id, new Pair<>(a1, b1));
                id++;
            }
            PriorityQueue<Pair<Integer, Integer>> extra_a = new PriorityQueue<>(), extra_b = new PriorityQueue<>();

            for (int i = a.size(); i < b.size(); i++) {
                extra_b.add(b.get(i));
                whoCare.add(new Pair<>(b.get(i).first, new Pair<>(b.get(i).second, 1)));
            }
            for (int i = b.size(); i < a.size(); i++) {
                extra_a.add(a.get(i));
                whoCare.add(new Pair<>(a.get(i).first, new Pair<>(a.get(i).second, 2)));
            }

            if (tot.size() < k) {
                w.printLine("-1");
                return;
            }
            long res = 0;
            int tot_book = 0;
            PriorityQueue<PP> customize = new PriorityQueue<>();
            PriorityQueue<PP> customize2 = new PriorityQueue<>();
            while (k-- > 0) {
                Pair<Integer, Pair<Integer, Integer>> pp = tot.poll();
                res += pp.first;
                if (pp.second.first != 0) {
                    tot_book += 2;
                    customize.add(new PP(pp.first, pp.second.first));
                    Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> pkp = kk.get(pp.second.first);
                    resInd.add(pkp.first.second);
                    resInd.add(pkp.second.second);
                } else {
                    tot_book++;
                    resInd.add(pp.second.second);
                    customize2.add(new PP(pp.first, pp.second.second));
                }
            }
//        w.printLine(res, tot_book);

//        w.printLine(customize);
//        System.out.println(customize.poll());
            if (tot_book > m) {

                PriorityQueue<Pair<Integer, Integer>> pp = new PriorityQueue<>();
                while (!tot.isEmpty()) {
                    Pair<Integer, Pair<Integer, Integer>> ppq = tot.poll();
                    if (ppq.second.first == 0) {
                        pp.add(new Pair<>(ppq.first, ppq.second.second));
                    }
                }
                while (!pp.isEmpty()) {
                    Pair<Integer, Integer> kt = pp.poll();
                    PP rem = customize.poll();
                    res -= rem.first;
                    Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> klk = kk.get(rem.second);
                    resInd.remove(klk.first.second);
                    resInd.remove(klk.second.second);
                    res += kt.first;
                    resInd.add(kt.second);
                    tot_book--;
                    if (tot_book == m) {
                        break;
                    }
                }
            } else if (tot_book == m) {

            } else {

                int great = 0;
                while (!tot.isEmpty()) {
                    Pair<Integer, Pair<Integer, Integer>> pk = tot.poll();
                    if (pk.second.first == 0) {
                        whoCare.add(new Pair<>(pk.first, new Pair<>(pk.second.second, 3)));
                    } else {
                        Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> ll = kk.get(pk.second.first);
                        whoCare.add(new Pair<>(ll.first.first, new Pair<>(ll.first.second, 2)));
                        extra_a.add(ll.first);
                        whoCare.add(new Pair<>(ll.second.first, new Pair<>(ll.second.second, 1)));
                        extra_b.add(ll.second);
                    }
                }
                Collections.sort(whoCare);

                // verify once
                int extra = 0, aaa = 0, bbb = 0;
                for (int i = 0; i < whoCare.size() && tot_book < m; i++, tot_book++) {
                    Pair<Integer, Pair<Integer, Integer>> pqp = whoCare.get(i);
                    res += pqp.first;
                    resInd.add(pqp.second.first);
                    if (pqp.second.second == 3) {
                        customize2.add(new PP(pqp.first, pqp.second.first));
                    } else if (pqp.second.second == 2) {
                        aaa++;
                        great++;
                    } else if (pqp.second.second == 1) {
                        great--;
                        bbb++;
                    }
                }

                while (great > 0) {
                    while (!extra_b.isEmpty() && resInd.contains(extra_b.peek().second) &&
                            !customize2.isEmpty() && customize2.peek().first < extra_b.peek().first) {

                        extra_b.poll();
                    }
                    if (customize2.isEmpty() || extra_b.isEmpty()) {
                        break;
                    }
                    Pair<Integer, Integer> p1 = extra_b.poll();
                    PP p2 = customize2.poll();
                    res -= p2.first;
                    resInd.remove(p2.second);
                    res += p1.first;
                    resInd.add(p1.second);
                    great--;
                }

                while (great < 0) {
                    while (!extra_a.isEmpty() && resInd.contains(extra_a.peek().second) &&
                            !customize2.isEmpty() && customize2.peek().first < extra_a.peek().first) {

                        extra_a.poll();
                    }
                    if (customize2.isEmpty() || extra_a.isEmpty()) {
                        break;
                    }
                    Pair<Integer, Integer> p1 = extra_a.poll();
                    PP p2 = customize2.poll();
                    res -= p2.first;
                    resInd.remove(p2.second);
                    res += p1.first;
                    resInd.add(p1.second);
                    great++;
                }

                extra = Math.min(aaa, bbb);
                while (extra-- > 0) {
                    while (!extra_a.isEmpty() && resInd.contains(extra_a.peek().second) &&
                            !customize2.isEmpty() && customize2.peek().first < extra_a.peek().first) {

                        extra_a.poll();
                    }
                    while (!extra_b.isEmpty() && resInd.contains(extra_b.peek().second) &&
                            !customize2.isEmpty() && customize2.peek().first < extra_b.peek().first) {

                        extra_b.poll();
                    }
                    if (customize2.isEmpty() || (extra_a.isEmpty() && extra_b.isEmpty())) {
                        break;
                    }
                    if (!extra_a.isEmpty()) {
                        Pair<Integer, Integer> p1 = extra_a.poll();
                        PP p2 = customize2.poll();
                        res -= p2.first;
                        resInd.remove(p2.second);
                        res += p1.first;
                        resInd.add(p1.second);
                    }
                    if (!extra_b.isEmpty()) {
                        Pair<Integer, Integer> p1 = extra_b.poll();
                        PP p2 = customize2.poll();
                        res -= p2.first;
                        resInd.remove(p2.second);
                        res += p1.first;
                        resInd.add(p1.second);
                    }
                }

            }
            if (tot_book == m) {
                w.printLine(res);
                for (int xx : resInd) {
                    w.print(xx + 1, "");
                }
                w.printLine();
            } else {
                w.printLine("-1");
            }

        }

    }

    static class PP implements Comparable<PP> {
        int first;
        int second;

        public PP(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public String toString() {
            return "PP{" +
                    "first=" + first +
                    ", second=" + second +
                    '}';
        }

        public int compareTo(PP o) {
            return o.first - this.first;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class Pair<U, V> implements Comparable<Pair<U, V>> {
        public final U first;
        public final V second;

        public Pair(U first, V second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Pair pair = (Pair) o;

            return !(first != null ? !first.equals(pair.first) : pair.first != null) &&
                    !(second != null ? !second.equals(pair.second) : pair.second != null);
        }

        public int hashCode() {
            int result = first != null ? first.hashCode() : 0;
            result = 31 * result + (second != null ? second.hashCode() : 0);
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        public int compareTo(Pair<U, V> o) {
            int value = ((Comparable<U>) first).compareTo(o.first);
            if (value != 0) {
                return value;
            }
            return ((Comparable<V>) second).compareTo(o.second);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine() {
            writer.println();
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

        public void printLine(long i) {
            writer.println(i);
        }

    }
}

