#include <bits/stdc++.h>
using namespace std;
struct s {
  int t, a, b;
  int pos1 = -1, pos2 = -1;
  bool check = false;
  int m_val = 1;
};
bool comp(s p, s q) {
  if (p.t < q.t) return true;
  return false;
}
bool comp1(s p, s q) {
  if (p.t < q.t)
    return true;
  else if (p.t == q.t) {
    if (p.m_val > q.m_val) return true;
    return false;
  } else
    return false;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  struct s all[n + 5];
  struct s alice[n + 5];
  struct s bob[n + 5];
  struct s none[n + 5];
  struct s combine[n + 5];
  int u = 0, v = 0, x = 0, y = 0;
  for (int i = 0; i < n; i++) {
    int p, q, r;
    cin >> p >> q >> r;
    if (q == 1 && r == 1) {
      all[u].t = p;
      all[u].a = q;
      all[u].b = r;
      all[u++].pos2 = i;
    } else if (q == 1 && r == 0) {
      alice[v].t = p;
      alice[v].a = q;
      alice[v].b = r;
      alice[v++].pos2 = i;
    } else if (q == 0 && r == 1) {
      bob[x].t = p;
      bob[x].a = q;
      bob[x].b = r;
      bob[x++].pos2 = i;
    } else {
      none[y].t = p;
      none[y].a = q;
      none[y].b = r;
      none[y++].pos2 = i;
    }
  }
  if (v > 1) sort(alice, alice + v, comp);
  if (x > 1) sort(bob, bob + x, comp);
  int w;
  for (w = 0; w < u; w++) {
    combine[w].t = all[w].t;
    combine[w].a = 1;
    combine[w].b = 1;
    combine[w].m_val = 1;
    combine[w].pos1 = w;
  }
  for (int i = 0, j = 0; i < v and j < x; i++, j++) {
    combine[w].t = alice[i].t + bob[j].t;
    combine[w].a = 1;
    combine[w].b = 1;
    combine[w].pos1 = i;
    combine[w++].m_val = 2;
  }
  if (w > 1) sort(combine, combine + w, comp1);
  for (int i = 0; i < w; i++)
    cout << combine[i].t << " " << combine[i].m_val << endl;
  long long sum = 0;
  int position[n + 5];
  int cnt = 0;
  int l = 0;
  int m_count = 0;
  for (int i = 0; i < w; i++) {
    if (cnt == k)
      break;
    else if (cnt < k) {
      if ((combine[i].m_val + m_count < m) ||
          (combine[i].m_val + m_count == m && cnt + 1 == k)) {
        sum += (long long)combine[i].t;
        m_count += combine[i].m_val;
        cnt++;
        if (combine[i].m_val == 2) {
          position[l++] = alice[combine[i].pos1].pos2;
          position[l++] = bob[combine[i].pos1].pos2;
          alice[combine[i].pos1].check = true;
          bob[combine[i].pos1].check = true;
        } else {
          position[l++] = all[combine[i].pos1].pos2;
          all[combine[i].pos1].check = true;
        }
      }
    }
  }
  if (cnt == k) {
    for (int i = 0; i < u; i++) {
      if (all[i].check == false) none[y++] = all[i];
    }
    for (int i = 0; i < v; i++) {
      if (alice[i].check == false) none[y++] = alice[i];
    }
    for (int i = 0; i < x; i++) {
      if (bob[i].check == false) none[y++] = bob[i];
    }
    sort(none, none + y, comp);
    for (int i = 0; i < y; i++) {
      if (m_count == m)
        break;
      else if (m_count < m) {
        sum += (long long)none[i].t;
        position[l++] = none[i].pos2;
        m_count++;
      }
    }
    if (m_count == m) {
      cout << sum << endl;
      sort(position, position + l);
      for (int i = 0; i < l; i++) cout << position[i] + 1 << " ";
      cout << endl;
    } else
      cout << -1 << endl;
  } else
    cout << -1 << endl;
  return 0;
}
