#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long arr[N], n, k, alice[N], bob[N], prea[N], preb[N], prec[N], m;
vector<pair<long long, long long>> a, b, c, d;
void func() {
  cin >> n >> m >> k;
  for (long long i = 1; i < n + 1; i++) cin >> arr[i] >> alice[i] >> bob[i];
  for (long long i = 1; i < n + 1; i++) {
    if (alice[i] && bob[i])
      c.push_back(make_pair(arr[i], i));
    else if (alice[i])
      a.push_back(make_pair(arr[i], i));
    else if (bob[i])
      b.push_back(make_pair(arr[i], i));
    else
      d.push_back(make_pair(arr[i], i));
  }
  if ((long long)c.size() + (long long)a.size() < k ||
      (long long)c.size() + (long long)b.size() < k) {
    cout << "-1\n";
    return;
  }
  a.push_back(make_pair(0LL, 0LL));
  b.push_back(make_pair(0LL, 0LL));
  c.push_back(make_pair(0LL, 0LL));
  d.push_back(make_pair(0LL, 0LL));
  sort(a.begin(), a.end());
  for (long long i = 1; i < (long long)a.size(); i++)
    prea[i] = prea[i - 1] + a[i].first;
  sort(b.begin(), b.end());
  for (long long i = 1; i < (long long)b.size(); i++)
    preb[i] = preb[i - 1] + b[i].first;
  sort(c.begin(), c.end());
  for (long long i = 1; i < (long long)c.size(); i++)
    prec[i] = prec[i - 1] + c[i].first;
  sort(d.begin(), d.end());
  long long zz = min(m, (long long)c.size() - 1);
  long long idx = -1;
  for (long long i = 0; i < zz + 1; i++) {
    long long rem = k - i;
    if (rem >= 0 && rem <= (long long)a.size() - 1 &&
        rem <= (long long)b.size() - 1 && i + 2 * rem <= m &&
        i + (long long)a.size() - 1 + (long long)b.size() - 1 +
                (long long)d.size() - 1 >=
            m) {
      idx = i;
      break;
    }
  }
  if (idx == -1) {
    cout << "-1\n";
    return;
  }
  long long rem = k - idx;
  set<pair<long long, long long>, greater<pair<long long, long long>>> xx;
  set<pair<long long, long long>> yy;
  for (long long i = rem + 1; i < (long long)a.size(); i++)
    yy.insert(make_pair(a[i].first, 0));
  for (long long i = rem + 1; i < (long long)b.size(); i++)
    yy.insert(make_pair(b[i].first, 1));
  for (long long i = 1; i < (long long)d.size(); i++)
    yy.insert(make_pair(d[i].first, 2));
  long long extra = m - (idx + 2 * rem);
  while (extra--) {
    xx.insert(*yy.begin());
    yy.erase(yy.begin());
  }
  long long sm1 = 0, sm2 = 0, sm3 = 0, pp = 0, qq = 0, rr = 0;
  for (auto i : xx) {
    if (i.second == 0) sm1 += i.first, pp++;
    if (i.second == 1) sm2 += i.first, qq++;
    if (i.second == 2) sm3 += i.first, rr++;
  }
  for (long long i = 1; i < rem + 1; i++) {
    sm1 += a[i].first;
    pp++;
    sm2 += b[i].first;
    qq++;
  }
  long long ans = LONG_LONG_MAX, cntc = -1, cnta = -1, cntb = -1, cntd = -1;
  for (long long i = idx; i < zz + 1; i++) {
    long long rem = k - i;
    if (rem < 0 || rem > (long long)a.size() - 1 ||
        rem > (long long)b.size() - 1)
      continue;
    if (i + 2 * rem <= m) {
      if (i + (long long)a.size() - 1 + (long long)b.size() - 1 +
              (long long)d.size() - 1 <
          m)
        continue;
      if (prec[i] + sm1 + sm2 + sm3 < ans) {
        ans = prec[i] + sm1 + sm2 + sm3;
        cntc = i;
        cnta = pp;
        cntb = qq;
        cntd = rr;
      }
    }
    if ((long long)xx.size() != 0) {
      pair<long long, long long> tt = *xx.begin();
      if (tt.second == 0) {
        pp--;
        sm1 -= tt.first;
      } else if (tt.second == 1) {
        qq--;
        sm2 -= tt.first;
      } else if (tt.second == 2) {
        rr--;
        sm3 -= tt.first;
      }
      xx.erase(xx.begin());
    }
  }
  if (ans == LONG_LONG_MAX) {
    cout << "-1\n";
    return;
  }
  cout << ans;
  cout << "\n";
  for (long long i = 1; i < cntc + 1; i++) cout << c[i].second << " ";
  for (long long i = 1; i < cntb + 1; i++) cout << b[i].second << " ";
  for (long long i = 1; i < cnta + 1; i++) cout << a[i].second << " ";
  for (long long i = 1; i < cntd + 1; i++) cout << d[i].second << " ";
  cout << "\n";
}
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long ntc = 1;
  for (long long i = 1; i < ntc + 1; i++) {
    func();
  }
  return 0;
}
