import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
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 revanth
 */
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);
        E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1ReadingBooksEasyVersion {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();
            ArrayList<Triple> al = new ArrayList<>();
            for (int i = 0; i < n; i++)
                al.add(new Triple(in.nextInt(), in.nextInt(), in.nextInt()));
            Collections.sort(al);
            int x = 0, y = 0, z = 0, i = 0, sum = 0;
            while (i < k) {
                for (; x < n; x++) {
                    if (al.get(x).y == 0 && al.get(x).z == 1)
                        break;
                }
                for (; y < n; y++) {
                    if (al.get(y).y == 1 && al.get(y).z == 0)
                        break;
                }
                for (; z < n; z++) {
                    if (al.get(z).y == 1 && al.get(z).z == 1)
                        break;
                }
                if (x == n && y == n && z == n)
                    break;
                out.println(x + " " + y + " " + z);
                if ((x == n || y == n) && z < n) {
                    sum += al.get(z).x;
                    z++;
                } else if (z == n && x < n && y < n) {
                    sum += al.get(x).x + al.get(y).x;
                    x++;
                    y++;
                } else {
                    if (al.get(x).x + al.get(y).x < al.get(z).x) {
                        sum += al.get(x).x + al.get(y).x;
                        x++;
                        y++;
                    } else {
                        sum += al.get(z).x;
                        z++;
                    }
                }
                i++;
            }
            out.println(i == k ? sum : "-1");
        }

    }

    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 println(Object... objects) {
            print(objects);
            writer.println();
        }

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

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int snumChars;
        private InputReader.SpaceCharFilter filter;

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

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

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

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            boolean isSpaceChar(int ch);

        }

    }

    static class Triple implements Comparable<Triple> {
        public int x;
        public int y;
        public int z;

        public Triple(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Triple triple = (Triple) o;
            return x == triple.x &&
                    y == triple.y &&
                    z == triple.z;
        }

        public int hashCode() {
            return Objects.hash(x, y, z);
        }

        public int compareTo(Triple t) {
            return Integer.compare(x, t.x);
        }

    }
}

