#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
void solve() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<Pll> ba, bb, bab, bno;
  for (ll i = 0; i < N; i++) {
    ll t, a, b;
    cin >> t >> a >> b;
    if (a == 1 && b == 1) {
      bab.emplace_back(t, i);
    } else if (a == 1) {
      ba.emplace_back(t, i);
    } else if (b == 1) {
      bb.emplace_back(t, i);
    } else {
      bno.emplace_back(t, i);
    }
  }
  sort(bab.begin(), bab.end());
  sort(ba.begin(), ba.end());
  sort(bb.begin(), bb.end());
  sort(bno.begin(), bno.end());
  ll nab = (ll)bab.size();
  ll na = (ll)ba.size();
  ll nb = (ll)bb.size();
  ll nno = (ll)bno.size();
  ll start = max(K - min(na, nb), 0ll);
  start = max(start, K * 2 - M);
  if (start > nab) {
    cout << -1 << "\n";
    return;
  }
  ll sab = 0, sa = 0, sb = 0;
  ll sr = 0;
  for (ll i = 0; i < start; i++) {
    sab += bab[i].first;
  }
  for (ll i = 0; i < K - start; i++) {
    sa += ba[i].first;
    sb += bb[i].first;
  }
  set<ll> st, st_used;
  for (ll i = K - start; i < na; i++) {
    st.emplace(ba[i].first);
  }
  for (ll i = K - start; i < nb; i++) {
    st.emplace(bb[i].first);
  }
  for (ll i = 0; i < nno; i++) {
    st.emplace(bno[i].first);
  }
  {
    ll cnt = 0;
    while (cnt < M - (K * 2 - start)) {
      auto itr = st.begin();
      sr += *itr;
      st_used.emplace(*itr);
      st.erase(itr);
      ++cnt;
    }
  }
  ll idx_mn = -1;
  ll s_mn = L_INF;
  for (ll i = start; i <= nab; i++) {
    ll t = sab + sa + sb + sr;
    if (t < s_mn) {
      s_mn = t;
      idx_mn = i;
    }
    if (i == nab) continue;
    ll d = max(K - i, 0ll);
    sab += bab[i].first;
    if (d > 0) {
      sa -= ba[d - 1].first;
      sb -= bb[d - 1].first;
      st.emplace(ba[d - 1].first);
      st.emplace(bb[d - 1].first);
      while (*st_used.rbegin() > *st.begin()) {
        auto itr = st.begin();
        st_used.emplace(*itr);
        sr += *itr;
        st.erase(itr);
        auto itr2 = st_used.rbegin().base();
        st.emplace(*itr2);
        sr -= *itr2;
        st_used.erase(itr2);
      }
    }
    st_used.emplace(*st.begin());
    sr += *st.begin();
    st.erase(st.begin());
  }
  Vl ans;
  for (ll i = 0; i < idx_mn; i++) {
    ans.emplace_back(bab[i].second);
  }
  for (ll i = 0; i < K - idx_mn; i++) {
    ans.emplace_back(ba[i].second);
    ans.emplace_back(bb[i].second);
  }
  set<Pll> stt;
  for (ll i = K - idx_mn; i < na; i++) {
    stt.emplace(ba[i]);
  }
  for (ll i = K - idx_mn; i < nb; i++) {
    stt.emplace(bb[i]);
  }
  for (ll i = 0; i < nno; i++) {
    stt.emplace(bno[i]);
  }
  {
    auto itr = stt.begin();
    for (ll i = 0; i < M - (K * 2 - idx_mn); i++) {
      ans.emplace_back((*itr).second);
      ++itr;
    }
  }
  cout << s_mn << "\n";
  for (const auto& v : ans) {
    cout << v + 1 << " ";
  }
  cout << "\n";
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  solve();
  cout << flush;
  return 0;
}
