#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t, n, m, k, i, j, a, b;
  cin >> n >> m >> k;
  vector<pair<int, int>> alice, bob, both, rest;
  set<pair<int, int>> free, taken;
  for (i = 0; i < n; ++i) {
    cin >> t >> a >> b;
    if (a && b) {
      both.push_back({t, i});
    } else if (a) {
      alice.push_back({t, i});
    } else if (b) {
      bob.push_back({t, i});
    } else {
      rest.push_back({t, i});
    }
  }
  sort(alice.begin(), alice.end());
  sort(bob.begin(), bob.end());
  sort(both.begin(), both.end());
  int both_sz = both.size();
  int a_sz = alice.size();
  int b_sz = bob.size();
  int ab_sz = min(a_sz, b_sz);
  if (ab_sz + both_sz < k) {
    cout << -1;
    return 0;
  }
  int k_start_both = max(0, k - ab_sz);
  int k_end_both = min(m, both_sz);
  for (i = k - k_start_both; i < a_sz; ++i) {
    free.insert(alice[i]);
  }
  for (i = k - k_start_both; i < b_sz; ++i) {
    free.insert(bob[i]);
  }
  for (auto v : rest) {
    free.insert(v);
  }
  int INF = 2e9 + 100;
  int ans_t = INF, ans_both = -1;
  int taken_sum = 0;
  vector<int> alice_sum(a_sz + 1), bob_sum(b_sz + 1), both_sum(both_sz + 1);
  for (i = 1; i <= a_sz; ++i) {
    alice_sum[i] += alice_sum[i - 1] + alice[i - 1].first;
  }
  for (i = 1; i <= b_sz; ++i) {
    bob_sum[i] += bob_sum[i - 1] + bob[i - 1].first;
  }
  for (i = 1; i <= both_sz; ++i) {
    both_sum[i] += both_sum[i - 1] + both[i - 1].first;
  }
  for (i = k_start_both; i <= k_end_both; ++i) {
    int tak_ab = max(0, k - i);
    int tak = i + 2 * tak_ab + taken.size();
    if (tak > m && (!taken.empty())) {
      auto f = taken.rbegin();
      taken.erase(*f);
      tak--;
    }
    while (tak < m && (!free.empty())) {
      auto f = free.begin();
      taken_sum += f->first;
      tak++;
      taken.insert(*f);
      free.erase(*f);
    }
    if (tak == m) {
      int cur_ans =
          both_sum[i] + alice_sum[tak_ab] + bob_sum[tak_ab] + taken_sum;
      if (cur_ans < ans_t) {
        ans_t = cur_ans;
        ans_both = i;
      }
    }
    assert((k - i) <= ab_sz);
    if (k - i - 1 >= 0) {
      auto av = alice[k - i - 1];
      auto bv = bob[k - i - 1];
      free.insert(av);
      if (!taken.empty()) {
        auto f = taken.rbegin();
        if (f->first > av.first) {
          taken.insert(av);
          free.insert(*f);
          free.erase(av);
          taken.erase(*f);
        }
      }
      free.insert(bv);
      if (!taken.empty()) {
        auto f = taken.rbegin();
        if (f->first > bv.first) {
          taken.insert(bv);
          free.insert(*f);
          free.erase(bv);
          taken.erase(*f);
        }
      }
    }
  }
  if (ans_t == INF) {
    cout << -1;
    return 0;
  }
  cout << ans_t << "\n";
  free.clear();
  int tak_ab = max(0, k - ans_both);
  for (i = tak_ab; i < a_sz; ++i) {
    free.insert(alice[i]);
  }
  for (i = tak_ab; i < b_sz; ++i) {
    free.insert(bob[i]);
  }
  for (auto v : rest) {
    free.insert(v);
  }
  for (i = 0; i < ans_both; ++i) {
    cout << (both[i].second + 1) << " ";
  }
  for (i = 0; i < tak_ab; ++i) {
    cout << (alice[i].second + 1) << " ";
    cout << (bob[i].second + 1) << " ";
  }
  int tak = ans_both + 2 * tak_ab;
  while (tak < m) {
    auto f = free.begin();
    cout << (f->second + 1) << " ";
    free.erase(f);
    tak++;
  }
  return 0;
}
