#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int mo = 2000000007;
pair<int, int> a[4][maxn];
int b[4][maxn], cnt[4];
int n, m, k, u, v, w;
int work(int v1, int v2) {
  int l2 = 1, r2 = m - v1 - v2, tp = m - v1 - v2;
  while (l2 < r2 + 1) {
    int mid = l2 + r2 >> 1;
    if (b[1][mid] + b[0][tp - mid] < b[1][mid + 1] + b[0][tp - mid - 1])
      r2 = mid;
    else
      l2 = mid + 1;
  }
  return min(b[1][l2] + b[0][tp - l2], b[1][r2] + b[0][tp - r2]);
}
struct rr {
  int a, b, c, d;
} tmp;
int main() {
  int ans = mo;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = (1); i <= (n); ++i) {
    scanf("%d%d%d", &u, &v, &w);
    a[w * 2 + v][++cnt[w * 2 + v]].first = u,
                               a[w * 2 + v][cnt[w * 2 + v]].second = i;
  }
  if (cnt[1] + cnt[3] < k || cnt[2] + cnt[3] < k || 2 * k - cnt[3] > m ||
      m < k) {
    printf("-1\n");
    exit(0);
  }
  for (int i = (0); i <= (3); ++i) sort(a[i] + 1, a[i] + 1 + cnt[i]);
  for (int i = (0); i <= (3); ++i) {
    for (int j = (1); j <= (cnt[i]); ++j) {
      b[i][j] = b[i][j - 1] + a[i][j].first;
    }
  }
  deque<int> q[4];
  int st = 0, val = 0;
  if (cnt[1] >= k && cnt[2] >= k && m >= 2 * k) {
    for (int i = (1); i <= (k); ++i)
      q[1].push_back(a[1][i].second), q[2].push_back(a[2][i].second);
    val = m - 2 * k;
    st = (val > cnt[0]) ? val - cnt[0] : 0;
    for (int i = (1); i <= (st); ++i) q[3].push_back(a[3][i].second);
  } else {
    st = max(k - min(cnt[1], cnt[2]), m - cnt[1] - cnt[2] - cnt[0]);
    if (m < 2 * k) st = 2 * k - m;
    for (int i = (1); i <= (k - st); ++i)
      q[2].push_back(a[2][i].second), q[1].push_back(a[1][i].second);
    for (int i = (1); i <= (st); ++i) q[3].push_back(a[3][i].second);
    val = m - 2 * k + st;
  }
  for (int i = (1); i <= (val); ++i) {
    int aa = a[1][q[1].size() + 1].first, bb = a[2][q[2].size() + 1].first,
        cc = a[0][q[0].size() + 1].first;
    if (aa == 0) aa = 1e9;
    if (bb == 0) bb = 1e9;
    if (cc == 0) cc = 1e9;
    if (aa < bb && aa < cc)
      q[1].push_back(a[1][i].second);
    else if (bb < aa && bb < cc)
      q[2].push_back(a[2][i].second);
    else if (cc < 1e5)
      q[0].push_back(a[0][i].second);
    else
      q[3].push_back(a[3][i].second);
  }
  ans = b[0][q[0].size()] + b[1][q[1].size()] + b[2][q[2].size()] +
        b[3][q[3].size()];
  tmp = {(int)q[0].size(), (int)q[1].size(), (int)q[2].size(),
         (int)q[3].size()};
  for (int i = (st + 1); i <= (cnt[3]); ++i) {
    q[3].push_back(st);
    q[2].pop_back(), q[1].pop_back();
    int aa = a[1][q[1].size() + 1].first, bb = a[2][q[2].size() + 1].first,
        cc = a[0][q[0].size() + 1].first;
    if (aa == 0) aa = 1e9;
    if (bb == 0) bb = 1e9;
    if (cc == 0) cc = 1e9;
    if (aa < bb && aa < cc)
      q[1].push_back(a[1][i].second);
    else if (bb < aa && bb < cc)
      q[2].push_back(a[2][i].second);
    else if (cc < 1e5)
      q[0].push_back(a[0][i].second);
    int cur = b[0][q[0].size()] + b[1][q[1].size()] + b[2][q[2].size()] +
              b[3][q[3].size()];
    if (cur < ans)
      cur = ans, tmp = {(int)q[0].size(), (int)q[1].size(), (int)q[2].size(),
                        (int)q[3].size()};
  }
  printf("%d\n", ans);
  for (int i = (1); i <= (tmp.a); ++i) printf("%d ", a[0][i].second);
  for (int i = (1); i <= (tmp.b); ++i) printf("%d ", a[1][i].second);
  for (int i = (1); i <= (tmp.c); ++i) printf("%d ", a[2][i].second);
  for (int i = (1); i <= (tmp.d); ++i) printf("%d ", a[3][i].second);
  return 0;
}
