#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> numa, numb, numc, numd;
vector<int> ansa, ansb, ansc, ansd;
vector<int> mema, memb;
int main(int argc, const char* argv[]) {
  int n, m, k;
  cin >> n >> m >> k;
  int cnta = 0, cntb = 0, cntc = 0;
  for (int i = 0; i < n; i++) {
    int temp, a, b;
    cin >> temp >> a >> b;
    if (a) {
      cnta++;
    }
    if (b) {
      cntb++;
    }
    if (a && b) {
      cntc++;
      numc.push_back({temp, i + 1});
    } else if (a) {
      numa.push_back({temp, i + 1});
    } else if (b) {
      numb.push_back({temp, i + 1});
    } else {
      numd.push_back({temp, i + 1});
    }
  }
  if (cnta < k || cntb < k) {
    cout << -1 << endl;
    return 0;
  }
  sort(numa.begin(), numa.end(), greater<pair<int, int>>());
  sort(numb.begin(), numb.end(), greater<pair<int, int>>());
  sort(numc.begin(), numc.end(), greater<pair<int, int>>());
  sort(numd.begin(), numd.end(), greater<pair<int, int>>());
  int cnt = 0;
  long long ans = 0, tmin = 1e6;
  if (numa.size() < k || numb.size() < k || 2 * k > m) {
    int tempa = k - numa.size(), tempb = k - numb.size();
    if (tempa < 0) {
      tempa = 0;
    }
    if (tempb < 0) {
      tempb = 0;
    }
    int ttemp = max(max(tempa, tempb), 2 * k - m);
    for (int i = 0; i < ttemp; i++) {
      ans += numc.back().first;
      ansc.push_back(numc.back().second);
      cnt++;
      numc.pop_back();
      cntc--;
    }
  }
  for (int i = cnt; i < k; i++) {
    ans += numb.back().first;
    ans += numa.back().first;
    ansa.push_back(numa.back().second);
    ansb.push_back(numb.back().second);
    mema.push_back(numa.back().first);
    memb.push_back(numb.back().first);
    numa.pop_back(), numb.pop_back();
  }
  cnt += 2 * (k - cnt);
  for (int i = cnt; i < m; i++) {
    ans += numd.back().first;
    ansd.push_back(numd.back().second);
    numd.pop_back();
  }
  if (ans) {
    if (ans < tmin) {
      tmin = ans;
    }
  }
  for (int i = 0; i < cntc; i++) {
    if (!mema.size() || !memb.size()) {
      break;
    }
    pair<int, int> tempa = {mema.back(), 0}, tempb = {memb.back(), 1},
                   tempd = {1e6, 2};
    int tempc = numc.back().first;
    if (numd.size()) {
      tempd = {numd.back().first, 2};
    }
    ans += tempc;
    ans -= tempa.first;
    ans -= tempb.first;
    auto ttmin = min(min(tempa, tempb), tempd);
    switch (ttmin.second) {
      case 0:
        ans += tempa.first;
        break;
      case 1:
        ans += tempb.first;
        break;
      case 2:
        ans += tempd.first;
        break;
      default:
        break;
    }
    if (ans < tmin) {
      ansc.push_back(numc.back().second);
      switch (ttmin.second) {
        case 0:
          ansb.pop_back();
          break;
        case 1:
          ansa.pop_back();
          break;
        case 2:
          ansa.pop_back();
          ansb.pop_back();
          ansd.push_back(numd.back().second);
          break;
        default:
          break;
      }
      tmin = ans;
    }
    numc.pop_back();
    mema.pop_back();
    memb.pop_back();
    if (numd.size()) {
      numd.pop_back();
    }
  }
  cout << tmin << endl;
  for (auto i : ansa) {
    cout << i << " ";
  }
  for (auto i : ansb) {
    cout << i << " ";
  }
  for (auto i : ansc) {
    cout << i << " ";
  }
  for (auto i : ansd) {
    cout << i << " ";
  }
  cout << endl;
  return 0;
}
