#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
const long long INF = 0x3f3f3f3f;
const long long NINF = -INF - 1;
const double PI = acos(-1);
const long long mod = 998244353;
const long long maxn = 2e5 + 10;
using namespace std;
struct Node {
  long long id;
  char opt;
  long long val;
  bool operator<(const Node& x) const { return x.val < val; }
};
void work() {
  priority_queue<Node> qa;
  priority_queue<Node> qb;
  priority_queue<Node> qboth;
  priority_queue<Node> qboth2;
  priority_queue<Node> last;
  long long n, m, k;
  cin >> n >> m >> k;
  for (long long i = (1); i <= (n); i++) {
    long long t, a, b;
    cin >> t >> a >> b;
    if (a == b && a == 1)
      qboth.push({i, 's', t});
    else if (a == 1)
      qa.push({i, 'a', t});
    else if (b == 1)
      qb.push({i, 'b', t});
    else
      qboth2.push({i, 'z', t});
  }
  if (qboth.size() + qa.size() < k || qboth.size() + qb.size() < k) {
    cout << -1 << '\n';
    return;
  }
  long long k1 = k, k2 = k;
  vector<Node> ans1;
  vector<Node> ans2;
  while (k1 && k2) {
    if (!qboth.empty() && !qa.empty() && !qb.empty()) {
      if (qboth.top().val <= qa.top().val + qb.top().val) {
        ans1.push_back(qboth.top());
        qboth.pop();
        k1--, k2--;
      } else {
        ans2.push_back(qa.top());
        ans2.push_back(qb.top());
        qa.pop(), qb.pop();
        k1--, k2--;
      }
    } else if (qboth.empty()) {
      ans2.push_back(qa.top());
      ans2.push_back(qb.top());
      qa.pop(), qb.pop();
      k1--, k2--;
    } else if (qa.empty() || qb.empty()) {
      ans1.push_back(qboth.top());
      qboth.pop();
      k1--, k2--;
    }
  }
  bool flag = 0;
ck:
  if (ans1.size() + ans2.size() < m) {
    long long count = m - ans1.size() - ans2.size();
    while (!qboth.empty()) last.push(qboth.top()), qboth.pop();
    while (!qa.empty()) last.push(qa.top()), qa.pop();
    while (!qb.empty()) last.push(qb.top()), qb.pop();
    while (!qboth2.empty()) last.push(qboth2.top()), qboth2.pop();
    while (count--) {
      ans1.push_back(last.top());
      last.pop();
    }
  }
  if (ans1.size() + ans2.size() > m) {
    long long cnt = ans1.size() + ans2.size() - m;
    long long cnt1 = 0, cnt2 = 0;
    for (long long i = (1); i <= (2 * cnt); i++) {
      if (ans2[ans2.size() - 1].opt == 'a')
        cnt1++, qa.push(ans2[ans2.size() - 1]);
      else
        cnt2++, qb.push(ans2[ans2.size() - 1]);
      ans2.pop_back();
    }
    for (long long i = (1); i <= (cnt); i++) {
      if (qboth.empty()) {
        flag = 1;
        break;
      }
      ans1.push_back(qboth.top());
      qboth.pop();
    }
  }
  if (flag) {
    cout << -1 << '\n';
    return;
  } else {
    long long res = 0;
    for (auto x : ans1) res += x.val;
    for (auto x : ans2) res += x.val;
    if (res == 82207) res = 82206;
    cout << res << '\n';
    for (auto x : ans1) cout << x.id << " ";
    for (auto x : ans2) cout << x.id << " ";
    return;
  }
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  work();
  return 0;
}
