#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<long long> sh, sp, rd, ot;
vector<pair<long long, long long> > shs, sps, rds, ots;
long long ax, bx, cx, viva;
long long arr[200005];
long long sum(long long i, long long j, long long s) {
  i = max(i, -1LL);
  j = max(j, -1LL);
  for (long long k = 20; k >= 0; k--) {
    long long x = i + (1 << k);
    if (x < shs.size() && shs[x].first <= s) {
      i = x;
    }
  }
  for (long long k = 20; k >= 0; k--) {
    long long x = j + (1 << k);
    if (x < sps.size() && sps[x].first <= s) {
      j = x;
    }
  }
  long long l = -1;
  for (long long k = 20; k >= 0; k--) {
    long long x = l + (1 << k);
    if (x < ots.size() && ots[x].first <= s) {
      l = x;
    }
  }
  ax = i;
  bx = j;
  cx = l;
  return i + 1 + j + 1 + l + 1;
}
long long get(long long i, long long j, long long tot) {
  i = max(i, 0LL);
  j = max(j, 0LL);
  if (i > sh.size()) return 1000000000000000000;
  if (j > sp.size()) return 1000000000000000000;
  if (i + j > tot) return 1000000000000000000;
  if (tot > sp.size() + sh.size() + ot.size()) return 1000000000000000000;
  if (tot <= 0) return 0;
  if (i + j == tot) {
    long long ret = 0;
    if (i) ret += sh[i - 1];
    if (j) ret += sp[j - 1];
    return ret;
  }
  long long x = 0;
  for (long long k = 20; k >= 0; k--) {
    long long cur = x + (1 << k);
    if (sum(i - 1, j - 1, cur) <= tot) {
      x = cur;
    }
  }
  viva = x;
  long long ans = 0;
  ans += (tot - sum(i - 1, j - 1, x)) * (x + 1);
  if (ax >= 0) ans += sh[ax];
  if (bx >= 0) ans += sp[bx];
  if (cx >= 0) ans += ot[cx];
  return ans;
}
void solve() {
  long long i, j, k, l, r, m, n, a, b, c, d, x, y, z, t;
  cin >> n >> m >> k;
  ax = bx = cx = -1;
  for (i = 0; i < n; i++) {
    cin >> x >> y >> z;
    if (y && z) {
      rd.push_back(x);
      rds.push_back({x, i + 1});
    } else if (y) {
      sh.push_back(x);
      shs.push_back({x, i + 1});
    } else if (z) {
      sp.push_back(x);
      sps.push_back({x, i + 1});
    } else {
      ot.push_back(x);
      ots.push_back({x, i + 1});
    }
    arr[i + 1] = x;
  }
  sort(rd.begin(), rd.end());
  sort(sh.begin(), sh.end());
  sort(sp.begin(), sp.end());
  sort(ot.begin(), ot.end());
  sort(rds.begin(), rds.end());
  sort(shs.begin(), shs.end());
  sort(sps.begin(), sps.end());
  sort(ots.begin(), ots.end());
  for (i = 1; i < rd.size(); i++) rd[i] += rd[i - 1];
  for (i = 1; i < sh.size(); i++) sh[i] += sh[i - 1];
  for (i = 1; i < sp.size(); i++) sp[i] += sp[i - 1];
  for (i = 1; i < ot.size(); i++) ot[i] += ot[i - 1];
  long long ans = get(k, k, m);
  long long flag = 0;
  for (i = 0; i < rd.size(); i++) {
    if (i + 1 > m) continue;
    long long cur = rd[i] + get(k - i - 1, k - i - 1, m - i - 1);
    if (cur < ans) {
      ans = cur;
      flag = 1;
      x = i;
    }
  }
  if (ans >= 1000000000000000) {
    cout << -1 << endl;
    return;
  }
  vector<long long> numb;
  if (flag) {
    get(k - x - 1, k - x - 1, m - i - 1);
    for (i = 0; i <= x; i++) {
      numb.push_back(rds[i].second);
    }
    sum(k - x - 2, k - x - 2, viva);
  } else {
    get(k, k, m);
    sum(k - 1, k - 1, viva);
  }
  for (i = 0; i <= ax; i++) numb.push_back(shs[i].second);
  for (i = 0; i <= bx; i++) numb.push_back(sps[i].second);
  for (i = 0; i <= cx; i++) numb.push_back(ots[i].second);
  if (flag)
    assert(numb.size() == (ax + bx + cx + x + 4));
  else
    assert(numb.size() == (ax + bx + cx + 3));
  vector<pair<long long, long long> > baal;
  for (i = ax + 1; i < shs.size(); i++) {
    baal.push_back(shs[i]);
  }
  for (i = bx + 1; i < sps.size(); i++) {
    baal.push_back(sps[i]);
  }
  for (i = cx + 1; i < ots.size(); i++) {
    baal.push_back(ots[i]);
  }
  sort(baal.begin(), baal.end());
  for (i = 0; i < baal.size(); i++) {
    if (numb.size() < m) {
      numb.push_back(baal[i].second);
    }
  }
  cout << ans << endl;
  for (auto &cv : numb) {
    cout << cv << ' ';
  }
  cout << endl;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
