#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, m, t, a, b, ab, c, no;
  long long int ans = 0;
  cin >> n >> m >> k;
  vector<pair<int, int> > alice, bob, alice_bob, none, res;
  for (int i = 1; i <= n; i++) {
    cin >> t >> a >> b;
    if (a == 1 && b == 1)
      alice_bob.push_back(make_pair(t, i));
    else if (a == 1)
      alice.push_back(make_pair(t, i));
    else if (b == 1)
      bob.push_back(make_pair(t, i));
    else
      none.push_back(make_pair(t, i));
  }
  long long int len_alice_bob = alice_bob.size(), len_alice = alice.size(),
                len_bob = bob.size();
  if (k > len_alice_bob + len_alice || k > len_alice_bob + len_bob ||
      m > len_alice_bob + len_bob + len_alice)
    cout << -1 << endl;
  else {
    sort(alice_bob.begin(), alice_bob.end());
    sort(alice.begin(), alice.end());
    sort(bob.begin(), bob.end());
    a = 0, b = 0, ab = 0, c = 0;
    while (k--) {
      c++;
      if (ab < len_alice_bob) {
        if (a < len_alice && b < len_bob) {
          if (alice_bob[ab].first > alice[a].first + bob[b].first &&
              c + 1 < m) {
            c++;
            res.push_back(make_pair(alice[a].first, alice[a].second));
            res.push_back(make_pair(bob[b].first, bob[b].second));
            ans = ans + alice[a++].first + bob[b++].first;
          } else {
            res.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second));
            ans = ans + alice_bob[ab++].first;
          }
        } else {
          res.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second));
          ans = ans + alice_bob[ab++].first;
        }
      } else {
        res.push_back(make_pair(alice[a].first, alice[a].second));
        res.push_back(make_pair(bob[b].first, bob[b].second));
        ans = ans + alice[a++].first + bob[b++].first;
      }
    }
    c = res.size();
    if (c < m) {
      c = m - c;
      while (a < len_alice) {
        none.push_back(make_pair(alice[a].first, alice[a].second));
        a++;
      }
      while (b < len_bob) {
        none.push_back(make_pair(bob[b].first, bob[b].second));
        b++;
      }
      while (ab < len_alice_bob) {
        none.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second));
        ab++;
      }
      int len_none = none.size();
      sort(none.begin(), none.end());
      no = 0;
      while (c--) {
        ans = ans + none[no].first;
        res.push_back(make_pair(none[no].first, none[no].second));
        no++;
      }
    }
    cout << ans << endl;
    for (int i = 0; i < m; i++) cout << res[i].second << " ";
    cout << endl;
  }
  return 0;
}
