import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Vector;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.io.InputStream;

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

  static class E2ReadingBooksHardVersion {
    public final void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      int k = in.nextInt();
      ArrayDeque<int[]> alice = new ArrayDeque<>();
      ArrayDeque<int[]> bob = new ArrayDeque<>();
      ArrayDeque<int[]> both = new ArrayDeque<>();
      ArrayDeque<int[]> none = new ArrayDeque<>();
      Stack<Integer> ansAlice = new Stack<>();
      Stack<Integer> ansBob = new Stack<>();
      Stack<Integer> ansBoth = new Stack<>();
      Stack<Integer> ansNone = new Stack<>();
      int[] t = new int[n];
      for (int i = 0; i < n; i++) {
        t[i] = in.nextInt();
        int a = in.nextInt();
        int b = in.nextInt();
        if (a == 1 && b == 1) {
          both.add(new int[] {t[i], i});
        } else if (a == 1) {
          alice.add(new int[] {t[i], i});
        } else if (b == 1) {
          bob.add(new int[] {t[i], i});
        } else {
          none.add(new int[] {t[i], i});
        }
      }
      sort(alice);
      sort(bob);
      sort(both);
      sort(none);

      int aliceCount = 0, bobCount = 0;
      while (aliceCount < k
          && bobCount < k
          && !(alice.isEmpty() && bob.isEmpty() && both.isEmpty())) {
        if (alice.isEmpty()) {
          if (both.isEmpty()) {
            break;
          }
          ansBoth.add(both.poll()[1]);
        } else if (bob.isEmpty()) {
          if (both.isEmpty()) {
            break;
          }
          ansBoth.add(both.poll()[1]);
        } else if (both.isEmpty()) {
          ansBob.add(bob.poll()[1]);
          ansAlice.add(alice.poll()[1]);
        } else {
          if (alice.peek()[0] + bob.peek()[0] < both.peek()[0]) {
            ansBob.add(bob.poll()[1]);
            ansAlice.add(alice.poll()[1]);
          } else {
            ansBoth.add(both.poll()[1]);
          }
        }
        aliceCount++;
        bobCount++;
      }
      while (aliceCount < k && !alice.isEmpty()) {
        ansAlice.add(alice.poll()[1]);
        ++aliceCount;
      }
      while (bobCount < k && !bob.isEmpty()) {
        ansBob.add(bob.poll()[1]);
        ++bobCount;
      }
      if (aliceCount < k || bobCount < k) {
        out.println(-1);
        return;
      }

      while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m
          && !both.isEmpty()
          && !ansAlice.isEmpty()
          && !ansBob.isEmpty()) {
        int ibob = ansBob.pop();
        int ialice = ansAlice.pop();
        bob.addFirst(new int[] {t[ibob], ibob});
        alice.addFirst(new int[] {t[ialice], ialice});
        ansBoth.add(both.poll()[1]);
      }
      if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m) {
        out.println(-1);
        return;
      }

      while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) {
        if (none.isEmpty()) {
          if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) {
            ansBob.add(bob.poll()[1]);
            ansAlice.add(alice.poll()[1]);
            ansBoth.pop();
          } else {
            break;
          }
        } else {
          if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) {
            int idxBoth = ansBoth.peek();
            int doubleCost = bob.peek()[0] + alice.peek()[0] - t[idxBoth];
            int noneCost = none.peek()[0];
            if (doubleCost < noneCost) {
              ansBob.add(bob.poll()[1]);
              ansAlice.add(alice.poll()[1]);
              ansBoth.pop();
            } else {
              ansNone.add(none.poll()[1]);
            }
          } else {
            ansNone.add(none.poll()[1]);
          }
        }
      }
      if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) {
        out.println(-1);
        return;
      }

      ArrayList<Integer> ansList = new ArrayList<>();
      ansList.addAll(ansBob);
      ansList.addAll(ansAlice);
      ansList.addAll(ansBoth);
      ansList.addAll(ansNone);
      if (ansList.size() != m) {
        throw new RuntimeException();
      }
      int ans = 0;
      for (int x : ansList) {
        ans += t[x];
      }
      out.println(ans);
      for (int x : ansList) {
        out.print((x + 1) + " ");
      }
      out.println();
    }

    private void sort(ArrayDeque<int[]> q) {
      ArrayList<int[]> a = new ArrayList<>(q);
      a.sort(Comparators.singletonIntArr);
      q.clear();
      q.addAll(a);
    }
  }

  static final class InputReader {
    private final InputStream stream;
    private final byte[] buf = new byte[1 << 16];
    private int curChar;
    private int numChars;

    public InputReader() {
      this.stream = System.in;
    }

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

    private final int read() {
      if (this.numChars == -1) {
        throw new UnknownError();
      } else {
        if (this.curChar >= this.numChars) {
          this.curChar = 0;

          try {
            this.numChars = this.stream.read(this.buf);
          } catch (IOException ex) {
            throw new InputMismatchException();
          }

          if (this.numChars <= 0) {
            return -1;
          }
        }

        return this.buf[this.curChar++];
      }
    }

    public final int nextInt() {
      int c;
      for (c = this.read(); isSpaceChar(c); c = this.read()) {}

      byte sgn = 1;
      if (c == 45) { // 45 == '-'
        sgn = -1;
        c = this.read();
      }

      int res = 0;

      while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
        res *= 10;
        res += c - 48; // 48 == '0'
        c = this.read();
        if (isSpaceChar(c)) {
          return res * sgn;
        }
      }

      throw new InputMismatchException();
    }

    private static final boolean isSpaceChar(final int c) {
      return c == 32 || c == 10 || c == 13 || c == 9
          || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t'
    }
  }

  static final class Comparators {
    public static final Comparator<int[]> singletonIntArr = (x, y) -> compare(x[0], y[0]);

    private static final int compare(final int x, final int y) {
      return x < y ? -1 : (x == y ? 0 : 1);
    }
  }
}
