#include <bits/stdc++.h>
const long long MAX = 200009, MOD = 1000000007, BASE = 311, inf = 1e17;
using namespace std;
struct node {
  long long t;
  long long a;
  long long b;
};
long long n, k, res, cnt, f, g, m;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    a, b, c, d;
vector<long long> q;
int main() {
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (v && w)
      a.push({u, i});
    else if (v)
      b.push({u, i});
    else if (w)
      c.push({u, i});
    else
      d.push({u, i});
  }
  while (1) {
    long long t1 = inf, t2 = inf;
    if (b.size())
      t1 = b.top().first;
    else
      t1 = inf;
    if (c.size())
      t1 += c.top().first;
    else
      t1 += inf;
    if (a.size()) t2 = a.top().first;
    if (a.size() && (t2 < t1 || cnt == m - 1)) {
      res += t2;
      q.push_back(a.top().second);
      if (a.size()) a.pop(), f++, g++;
      cnt++;
    } else {
      res += t1;
      if (b.size()) cnt++, g++, q.push_back(b.top().second), b.pop();
      if (c.size()) q.push_back(c.top().second), c.pop(), cnt++, f++;
    }
    if (f >= k && g >= k) break;
    if (a.empty() && b.empty() && c.empty()) break;
  }
  if (res < inf) {
    while (cnt < m && f >= k && g >= k) {
      vector<pair<long long, long long>> t(5, make_pair(inf, -1));
      if (a.size()) t[1] = a.top();
      if (b.size()) t[2] = b.top();
      if (c.size()) t[3] = c.top();
      if (d.size()) t[4] = d.top();
      sort(t.begin() + 1, t.begin() + 1 + 4);
      res += t[1].first, cnt++, q.push_back(t[1].second);
      if (a.size() && a.top().second == t[1].second)
        a.pop();
      else if (b.size() && b.top().second == t[1].second)
        b.pop();
      else if (c.size() && c.top().second == t[1].second)
        c.pop();
      else if (d.size() && d.top().second == t[1].second)
        d.pop();
      if (a.empty() && b.empty() && c.empty() && d.empty()) break;
    }
    if (f < k) {
      while (cnt < m) {
        pair<long long, long long> t[5];
        if (a.size()) t[1] = a.top();
        if (c.size()) t[2] = c.top();
        if (t[1].first < t[2].first)
          q.push_back(t[1].second), res += t[1].first, cnt++, a.pop(), f++;
        else
          q.push_back(t[2].second), res += t[2].first, cnt++, c.pop(), f++;
        if (a.empty() && c.empty()) break;
      }
    } else if (g < k) {
      while (cnt < m) {
        pair<long long, long long> t[5];
        if (a.size()) t[1] = a.top();
        if (b.size()) t[2] = b.top();
        if (t[1].first < t[2].first)
          q.push_back(t[1].second), res += t[1].first, cnt++, a.pop(), g++;
        else
          q.push_back(t[2].second), res += t[2].first, cnt++, b.pop(), g++;
        if (a.empty() && b.empty()) break;
      }
    }
    if (res >= inf || cnt < m || f < k || g < k)
      cout << -1 << endl;
    else {
      cout << res << endl;
      for (auto e : q) cout << e << " ";
    }
  } else
    cout << -1 << endl;
  return 0;
}
