#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const int MAX = 100005;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m, k;
  cin >> n >> m >> k;
  priority_queue<int, vector<int>, greater<int> > pq, pa, push_back, qp, temp;
  long long int t[n], a[n], b[n];
  multimap<long long int, long long int> m1, ma, mb, m0;
  vector<long long int> v;
  long long int i, j;
  for (i = 0; i < n; i++) {
    cin >> t[i] >> a[i] >> b[i];
    if (a[i] == 1 && b[i] == 1) {
      pq.push(t[i]);
      m1.insert(make_pair(t[i], i + 1));
    } else if (a[i] == 0 && b[i] == 1) {
      push_back.push(t[i]);
      mb.insert(make_pair(t[i], i + 1));
    } else if (a[i] == 1 && b[i] == 0) {
      pa.push(t[i]);
      ma.insert(make_pair(t[i], i + 1));
    } else {
      qp.push(t[i]);
      m0.insert(make_pair(t[i], i + 1));
    }
  }
  if ((pq.size() + pa.size() < k) || (pq.size() + push_back.size() < k)) {
    cout << "-1";
    return 0;
  }
  long long int k1 = k, k2 = k;
  long long int ans = 0, cn = 0;
  long long int op = 2 * k - m;
  if (op > 0) {
    if (pq.size() < op) {
      cout << "-1\n";
      return 0;
    }
    while (op--) {
      ans += pq.top();
      auto it = m1.lower_bound(pq.top());
      v.push_back(it->second);
      m1.erase(it);
      pq.pop();
      k1--;
      k2--;
      cn++;
    }
  }
  while (k1 || k2) {
    if (!pq.empty() && !pa.empty() && !push_back.empty()) {
      if (pq.top() < (pa.top() + push_back.top())) {
        ans += pq.top();
        auto it = m1.lower_bound(pq.top());
        v.push_back(it->second);
        m1.erase(it);
        pq.pop();
        k1--;
        k2--;
        cn++;
      } else {
        ans += pa.top() + push_back.top();
        auto it = ma.lower_bound(pa.top());
        v.push_back(it->second);
        ma.erase(it);
        it = mb.lower_bound(push_back.top());
        v.push_back(it->second);
        mb.erase(it);
        pa.pop();
        push_back.pop();
        k1--;
        k2--;
        cn += 2;
      }
    } else if (pq.empty()) {
      if (k1 > 0 && !pa.empty()) {
        ans += pa.top();
        auto it = ma.lower_bound(pa.top());
        v.push_back(it->second);
        ma.erase(it);
        pa.pop();
        k1--;
        cn++;
      }
      if (k2 > 0 && !push_back.empty()) {
        ans += push_back.top();
        auto it = mb.lower_bound(push_back.top());
        v.push_back(it->second);
        mb.erase(it);
        push_back.pop();
        k2--;
        cn++;
      }
    } else if (!pq.empty()) {
      ans += pq.top();
      auto it = m1.lower_bound(pq.top());
      v.push_back(it->second);
      m1.erase(it);
      pq.pop();
      cn++;
      k1--;
      k2--;
    }
  }
  if (cn > m) {
    cout << "-1\n";
    return 0;
  }
  while (!pq.empty()) {
    qp.push(pq.top());
    auto it = m1.lower_bound(pq.top());
    m0.insert(make_pair(pq.top(), it->second));
    m1.erase(it);
    pq.pop();
  }
  while (!pa.empty()) {
    qp.push(pa.top());
    auto it = ma.lower_bound(pa.top());
    m0.insert(make_pair(pa.top(), it->second));
    ma.erase(it);
    pa.pop();
  }
  while (!push_back.empty()) {
    qp.push(push_back.top());
    auto it = mb.lower_bound(push_back.top());
    m0.insert(make_pair(push_back.top(), it->second));
    mb.erase(it);
    push_back.pop();
  }
  k1 = m - cn;
  while (k1 > 0) {
    ans += qp.top();
    auto it = m0.lower_bound(qp.top());
    v.push_back(it->second);
    m0.erase(it);
    qp.pop();
    k1--;
  }
  cout << ans << endl;
  for (i = 0; i < v.size(); i++) cout << v[i] << " ";
  return 0;
}
