#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long cnt, sum;
  Node operator+(Node n) const { return {cnt + n.cnt, sum + n.sum}; }
};
long long N, M, K;
vector<pair<long long, long long> > v[2][2], w;
Node seg[33000];
bool vis[10101];
void Update(long long idx, long long val, long long n, long long l,
            long long r) {
  if (r < idx || idx < l) return;
  if (l == r) {
    seg[n].cnt += 1 * val;
    seg[n].sum += r * val;
    return;
  }
  long long mid = l + r >> 1;
  Update(idx, val, n << 1, l, mid);
  Update(idx, val, n << 1 | 1, mid + 1, r);
  seg[n].cnt = seg[n << 1].cnt + seg[n << 1 | 1].cnt;
  seg[n].sum = seg[n << 1].sum + seg[n << 1 | 1].sum;
}
Node Query(long long L, long long R, long long n, long long l, long long r) {
  if (r < L || R < l) return {0, 0};
  if (L <= l && r <= R) return seg[n];
  long long mid = l + r >> 1;
  return Query(L, R, n << 1, l, mid) + Query(L, R, n << 1 | 1, mid + 1, r);
}
long long Kth(long long k, long long n, long long l, long long r) {
  if (l == r) return r;
  long long mid = l + r >> 1;
  if (k <= seg[n << 1].cnt) return Kth(k, n << 1, l, mid);
  return Kth(k - seg[n << 1].cnt, n << 1 | 1, mid + 1, r);
}
signed main() {
  scanf("%lld %lld %lld", &N, &M, &K);
  for (long long i = 1; i <= N; i++) {
    long long t, a, b;
    scanf("%lld %lld %lld", &t, &a, &b);
    v[a][b].push_back({t, i});
    w.push_back({t, i});
    Update(t, 1, 1, 0, 10000);
  }
  if (v[1][1].size() + v[1][0].size() < K ||
      v[1][1].size() + v[0][1].size() < K) {
    printf("-1\n");
    return 0;
  }
  for (long long i = 0; i < 2; i++)
    for (long long j = 0; j < 2; j++) sort(v[i][j].begin(), v[i][j].end());
  sort(w.begin(), w.end());
  long long S11 = 0, S10 = 0, S01 = 0, j = 0, k = 0;
  for (long long i = 0; i < min(K, (long long)v[1][1].size()); i++) {
    S11 += v[1][1][i].first;
    Update(v[1][1][i].first, -1, 1, 0, 10000);
  }
  long long res = 2100000000, idx = -1;
  for (long long i = min(K, (long long)v[1][1].size()); i >= 0; i--) {
    if (v[1][0].size() < K - i || v[0][1].size() < K - i) break;
    if (i + K - i + K - i > M) continue;
    while (j < K - i) {
      S10 += v[1][0][j].first;
      Update(v[1][0][j].first, 1, 1, 0, 10000);
      j++;
    }
    while (k < K - i) {
      S01 += v[0][1][k].first;
      Update(v[0][1][k].first, 1, 1, 0, 10000);
      k++;
    }
    long long cnt, pos, kcnt, add;
    cnt = M - (i + K - i + K - i);
    if (cnt > 0) {
      pos = Kth(cnt, 1, 0, 10000);
      kcnt = cnt - Query(0, pos - 1, 1, 0, 10000).cnt;
      add = Query(0, pos - 1, 1, 0, 10000).sum + kcnt * pos;
    } else
      pos = kcnt = add = 0;
    long long val = S11 + S10 + S01 + add;
    if (res > val) {
      res = val;
      idx = i;
    }
    if (i) {
      S11 -= v[1][1][i - 1].first;
      Update(v[1][1][i - 1].first, -1, 1, 0, 10000);
    }
  }
  if (idx == -1) {
    printf("-1\n");
    return 0;
  }
  printf("%lld\n", res);
  vector<long long> ans;
  for (long long i = 0; i < idx; i++) {
    ans.push_back(v[1][1][i].second);
    vis[v[1][1][i].second] = true;
  }
  for (long long i = 0; i < K - idx; i++) {
    ans.push_back(v[1][0][i].second);
    vis[v[1][0][i].second] = true;
  }
  for (long long i = 0; i < K - idx; i++) {
    ans.push_back(v[0][1][i].second);
    vis[v[0][1][i].second] = true;
  }
  long long cnt = M - (idx + K - idx + K - idx);
  for (long long i = 0; i < w.size(); i++) {
    if (vis[w[i].second]) continue;
    if (cnt > 0) {
      ans.push_back(w[i].second);
      vis[w[i].second] = true;
      cnt--;
    }
  }
  for (long long x : ans) printf("%lld ", x);
  return 0;
}
