#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
long long qpow(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
int t[maxn], a[maxn], b[maxn], tot, tot1, atot, btot, lis[maxn], ltot;
struct Node {
  int v, id;
} aa[maxn], bb[maxn], sam[maxn], vac[maxn];
bool cmp(Node a, Node b) { return a.v < b.v; }
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (register int i = (0); i < (n); ++i) {
    scanf("%d%d%d", &t[i], &a[i], &b[i]);
    if (a[i] && b[i])
      sam[tot++] = {t[i], i + 1};
    else if (a[i])
      aa[atot++] = {t[i], i + 1};
    else if (b[i])
      bb[btot++] = {t[i], i + 1};
    else
      vac[tot1++] = {t[i], i + 1};
  }
  long long ans = 0;
  sort(aa, aa + atot, cmp);
  sort(bb, bb + btot, cmp);
  sort(sam, sam + tot, cmp);
  sort(vac, vac + tot1, cmp);
  if (atot + tot < k || btot + tot < k || atot + tot + btot + tot1 < m)
    return printf("-1\n"), 0;
  int i = 0, j = 0, cnt = 0;
  while (k--) {
    if (i == min(atot, btot) || j < tot && aa[i].v + bb[i].v > sam[j].v) {
      ans += (long long)sam[j++].v;
      cnt++;
    } else {
      ans += (long long)aa[i].v + (long long)bb[i].v;
      i++;
      cnt += 2;
    }
  }
  int i1 = i, i2 = i;
  bool failed = false;
  if (cnt > m) {
    cnt -= m;
    i--;
    k = 0;
    while (cnt--) {
      if (i == -1 || j == tot) {
        failed = true;
        break;
      }
      ans += (long long)sam[j].v - (long long)aa[i].v - (long long)bb[i].v;
      i--, j++;
    }
    i++;
    i1 = i2 = i;
  } else if (cnt < m) {
    cnt = m - cnt;
    j--;
    k = 0;
    i1 = i, i2 = i;
    while (cnt--) {
      if (k == tot1 && i < min(atot, btot) ||
          i < min(atot, btot) && aa[i].v + bb[i].v - sam[j].v < vac[k].v) {
        ans += (long long)aa[i].v + (long long)bb[i].v - (long long)sam[j].v;
        i++;
        j--;
        i1++, i2++;
      } else if (i < min(atot, btot) && k < tot1 &&
                 aa[i].v + bb[i].v - sam[j].v >= vac[k].v) {
        ans += (long long)vac[k].v;
        k++;
      } else if (i1 < atot && (i2 == btot || aa[i1].v <= bb[i2].v) &&
                 (k == tot1 || aa[i1].v <= vac[k].v)) {
        ans += (long long)aa[i1++].v;
      } else if (i2 < btot && (i1 == atot || bb[i2].v <= aa[i1].v) &&
                 (k == tot1 || bb[i2].v <= vac[k].v)) {
        ans += (long long)bb[i2++].v;
      } else
        ans += (long long)vac[k++].v;
    }
    j++;
  }
  if (failed)
    printf("-1\n");
  else {
    printf("%lld\n", ans);
    for (register int c = (0); c < (i1); ++c) printf("%d ", aa[c].id);
    for (register int c = (0); c < (i2); ++c) printf("%d ", bb[c].id);
    for (register int c = (0); c < (j); ++c) printf("%d ", sam[c].id);
    for (register int c = (0); c < (k); ++c) printf("%d ", vac[c].id);
    printf("\n");
  }
}
