#include <bits/stdc++.h>
using namespace std;
int A[2][200005], B[2][200005], Bot[2][200005], Bn[2][200005];
int bo[200005];
struct inb {
  int ind;
  int time;
} As[200005], Bs[200005], Bots[200005], Bns[200005];
bool cmp(inb a, inb b) { return a.time < b.time; }
char comp(int ai, int bi, int boti, int bni) {
  int min = 10000;
  if (A[1][ai] > 0 && A[1][ai] < min) {
    min = A[1][ai];
  }
  if (B[1][bi] > 0 && B[1][bi] < min) {
    min = B[1][bi];
  }
  if (Bot[1][boti] > 0 && Bot[1][boti] < min) {
    min = Bot[1][boti];
  }
  if (Bn[1][bni] > 0 && Bn[1][bni] < min) min = Bn[1][bni];
  if (min == A[1][ai]) return 'a';
  if (min == B[1][bi]) return 'b';
  if (min == Bot[1][boti]) return 'o';
  if (min == Bn[1][bni]) return 'n';
  return 'f';
}
int main() {
  int n, m, k;
  int t, a, b;
  scanf("%d %d %d", &n, &m, &k);
  int ae, be, bote, bne;
  ae = be = bote = bne = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &t, &a, &b);
    if (a == 1 && b == 1) {
      Bots[bote].ind = i + 1;
      Bot[0][bote] = i + 1;
      Bots[bote].time = t;
      Bot[1][bote++] = t;
    } else if (a == 1 && b == 0) {
      As[ae].ind = i + 1;
      As[ae].time = t;
      A[0][ae] = i + 1;
      A[1][ae++] = t;
    } else if (b == 1 && a == 0) {
      Bs[be].ind = i + 1;
      Bs[be].time = t;
      B[0][be] = i + 1;
      B[1][be++] = t;
    } else {
      Bns[bne].ind = i + 1;
      Bns[bne].time = t;
      Bn[0][bne] = i + 1;
      Bn[1][bne++] = t;
    }
  }
  sort(As, As + ae, cmp);
  sort(Bs, Bs + be, cmp);
  sort(Bots, Bots + bote, cmp);
  sort(Bns, Bns + bne, cmp);
  for (int i = 0; i < ae; i++) {
    A[0][i] = As[i].ind;
    A[1][i] = As[i].time;
  }
  for (int i = 0; i < be; i++) {
    B[0][i] = Bs[i].ind;
    B[1][i] = Bs[i].time;
  }
  for (int i = 0; i < bote; i++) {
    Bot[0][i] = Bots[i].ind;
    Bot[1][i] = Bots[i].time;
  }
  for (int i = 0; i < bne; i++) {
    Bn[0][i] = Bns[i].ind;
    Bn[1][i] = Bns[i].time;
  }
  int ik = 0, boti = 0, ai = 0, bi = 0, bni = 0;
  int ans = 0, boi = 0, cat = 0, ka = 0, kb = 0;
  int mab = ae;
  if (be < ae) mab = be;
  if (bote + mab < k || bote < 2 * k - m) {
    printf("%d\n", -1);
  } else {
    while (ik <= k - 1) {
      if (cat < m - k) {
        if (boti < bote) {
          if (ai < ae && bi < be) {
            if (ka > 0) {
              if (B[1][bi] < Bot[1][boti]) {
                ik++;
                bo[boi++] = B[0][bi];
                ans += B[1][bi++];
                ka--;
              } else {
                ik++;
                bo[boi++] = Bot[0][boti];
                ans += Bot[1][boti++];
              }
            } else if (kb > 0) {
              if (A[1][ai] < Bot[1][boti]) {
                ik++;
                bo[boi++] = A[0][ai];
                ans += A[1][ai++];
                kb--;
              } else {
                ik++;
                bo[boi++] = Bot[0][boti];
                ans += Bot[1][boti++];
              }
            } else {
              char mc = comp(ai, bi, boti, bni);
              int mi, bt;
              switch (mc) {
                case 'a':
                  mi = A[1][ai];
                  bt = A[0][ai];
                  break;
                case 'b':
                  mi = B[1][bi];
                  bt = B[0][bi];
                  break;
                case 'o':
                  mi = Bot[1][boti];
                  bt = Bot[0][boti];
                  break;
                case 'n':
                  mi = Bn[1][bni];
                  bt = Bn[0][bni];
                  break;
                default:
                  break;
              }
              if (Bot[1][boti] + mi <= (A[1][ai] + B[1][bi])) {
                ik++;
                bo[boi++] = Bot[0][boti];
                if (mc == 'a') {
                  ka++;
                  cat++;
                  bo[boi++] = A[0][ai];
                  ans += Bot[1][boti++] + A[1][ai++];
                } else if (mc == 'b') {
                  kb++;
                  cat++;
                  bo[boi++] = B[0][bi];
                  ans += Bot[1][boti++] + B[1][bi++];
                } else if (mc == 'o') {
                  ans += Bot[1][boti++];
                } else if (mc == 'n') {
                  cat++;
                  bo[boi++] = Bn[0][bni];
                  ans += Bot[1][boti++] + Bn[1][bni++];
                }
              } else {
                ik++;
                cat++;
                bo[boi++] = A[0][ai];
                bo[boi++] = B[0][bi];
                ans += A[1][ai++] + B[1][bi++];
              }
            }
          } else {
            ik++;
            bo[boi++] = Bot[0][boti];
            ans += Bot[1][boti++];
          }
        } else {
          ik++;
          if (ka--) {
            bo[boi++] = B[0][bi];
            ans += B[1][bi++];
          } else if (kb--) {
            bo[boi++] = A[0][ai];
            ans += A[1][ai++];
          } else {
            cat++;
            bo[boi++] = A[0][ai];
            bo[boi++] = B[0][bi];
            ans += A[1][ai++] + B[1][bi++];
          }
        }
      } else {
        ik++;
        bo[boi++] = Bot[0][boti];
        ans += Bot[1][boti++];
      }
    }
    while (boi < m) {
      char mc = comp(ai, bi, boti, bni);
      int mi, bt;
      switch (mc) {
        case 'a':
          mi = A[1][ai];
          bt = A[0][ai++];
          break;
        case 'b':
          mi = B[1][bi];
          bt = B[0][bi++];
          break;
        case 'o':
          mi = Bot[1][boti];
          bt = Bot[0][boti++];
          break;
        case 'n':
          mi = Bn[1][bni];
          bt = Bn[0][bni++];
          break;
        default:
          break;
      }
      ans += mi;
      ik++;
      bo[boi++] = bt;
    }
    printf("%d\n", ans);
    for (int i = 0; i < m - 1; i++) {
      printf("%d ", bo[i]);
    }
    printf("%d\n", bo[m - 1]);
  }
  return 0;
}
