#include <bits/stdc++.h>
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
  os << "[";
  for (auto it : v) os << it << " ";
  return os << "]";
}
using namespace std;
using namespace std;
struct DS {
  priority_queue<int> small;
  priority_queue<int, vector<int>, greater<int>> big;
  int sz;
  long long tot;
  DS(int n) : sz(n), tot(0) {}
  void fix() {
    while (int((small).size()) > sz) {
      assert(int((small).size()) > 0);
      big.push(small.top());
      tot -= small.top();
      small.pop();
    }
    while (int((small).size()) < sz && int((big).size()) > 0) {
      small.push(big.top());
      tot += big.top();
      big.pop();
    }
  }
  void resize(int s) {
    s = max(0, s);
    sz = min(s, int((big).size()) + int((small).size()));
    fix();
  }
  void insert(int x) {
    small.push(x);
    tot += x;
    fix();
  }
  int size() { return int((small).size()); }
};
int solve(vector<int> both, vector<int> alice, vector<int> bob,
          vector<int> other, int m, int k) {
  long long ans = -1;
  DS st(m);
  for (auto it : other) st.insert(it);
  vector<int> aliceTaken;
  vector<int> bobTaken;
  long long cur = 0;
  for (int i = 0; i < k; i++) {
    if (int((alice).size()) == 0 || int((bob).size()) == 0) break;
    aliceTaken.push_back(alice.back());
    bobTaken.push_back(bob.back());
    alice.pop_back();
    bob.pop_back();
    cur += aliceTaken.back();
    cur += bobTaken.back();
  }
  for (auto it : alice) st.insert(it);
  for (auto it : bob) st.insert(it);
  st.resize(max(0, m - 2 * int((aliceTaken).size())));
  if (int((st).size()) + 2 * int((aliceTaken).size()) == m &&
      int((aliceTaken).size()) == k)
    ans = st.tot + cur;
  vector<int> bothTaken;
  ;
  ;
  for (int i = 0; i < int((both).size()); i++) {
    bothTaken.push_back(both[i]);
    cur += both[i];
    if (int((bothTaken).size()) + int((aliceTaken).size()) < k) continue;
    if (int((bothTaken).size()) + int((aliceTaken).size()) > k &&
        int((aliceTaken).size()) > 0) {
      st.insert(aliceTaken.back());
      st.insert(bobTaken.back());
      cur -= aliceTaken.back();
      cur -= bobTaken.back();
      aliceTaken.pop_back();
      bobTaken.pop_back();
    }
    st.resize(m - int((bothTaken).size()) - 2 * int((aliceTaken).size()));
    if (int((st).size()) + int((bothTaken).size()) +
            2 * int((aliceTaken).size()) ==
        m) {
      if (ans == -1)
        ans = cur + st.tot;
      else
        ans = min(ans, cur + st.tot);
    }
  }
  return ans;
}
map<int, vector<int>> bothMp, aliceMp, bobMp, OtherMp;
void solve2(vector<int> both, vector<int> alice, vector<int> bob,
            vector<int> other, int m, int k, int finalAns) {
  long long ans = -1;
  ;
  ;
  DS st(m);
  for (auto it : other) st.insert(it);
  vector<int> aliceTaken, bobTaken, bothTaken;
  long long cur = 0;
  for (int i = 0; i < k; i++) {
    if (int((alice).size()) == 0 || int((bob).size()) == 0) break;
    aliceTaken.push_back(alice.back());
    bobTaken.push_back(bob.back());
    alice.pop_back();
    bob.pop_back();
    cur += aliceTaken.back();
    cur += bobTaken.back();
  }
  for (auto it : alice) st.insert(it);
  for (auto it : bob) st.insert(it);
  st.resize(max(0, m - 2 * int((aliceTaken).size())));
  if (int((st).size()) + 2 * int((aliceTaken).size()) == m &&
      int((aliceTaken).size()) == k)
    ans = st.tot + cur;
  auto PrintAns = [&]() {
    for (auto it : bothTaken) {
      cout << bothMp[it].back() << " ";
      bothMp[it].pop_back();
    }
    for (auto it : aliceTaken) {
      cout << aliceMp[it].back() << " ";
      aliceMp[it].pop_back();
    }
    for (auto it : bobTaken) {
      cout << bobMp[it].back() << " ";
      bobMp[it].pop_back();
    }
    while (int((st.small).size())) {
      auto it = st.small.top();
      st.small.pop();
      if (OtherMp[it].size()) {
        cout << OtherMp[it].back() << " ";
        OtherMp[it].pop_back();
      } else if (bobMp[it].size()) {
        cout << bobMp[it].back() << " ";
        bobMp[it].pop_back();
      } else if (aliceMp[it].size()) {
        cout << aliceMp[it].back() << " ";
        aliceMp[it].pop_back();
      }
    }
  };
  ;
  ;
  if (ans == finalAns) {
    ;
    ;
    PrintAns();
    return;
  }
  for (int i = 0; i < int((both).size()); i++) {
    bothTaken.push_back(both[i]);
    cur += both[i];
    if (int((bothTaken).size()) + int((aliceTaken).size()) < k) continue;
    if (int((bothTaken).size()) + int((aliceTaken).size()) > k &&
        int((aliceTaken).size()) > 0) {
      st.insert(aliceTaken.back());
      st.insert(bobTaken.back());
      cur -= aliceTaken.back();
      cur -= bobTaken.back();
      aliceTaken.pop_back();
      bobTaken.pop_back();
    }
    st.resize(m - int((bothTaken).size()) - 2 * int((aliceTaken).size()));
    if (int((st).size()) + int((bothTaken).size()) +
            2 * int((aliceTaken).size()) ==
        m) {
      if (ans == -1)
        ans = cur + st.tot;
      else
        ans = min(ans, cur + st.tot);
      if (ans == finalAns) {
        PrintAns();
        return;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    ;
    ;
    vector<int> both, alice, bob, other;
    for (int i = 0; i < n; i++) {
      int tt, a, b;
      cin >> tt >> a >> b;
      if (a && b) {
        both.push_back(tt);
        bothMp[tt].push_back(i + 1);
      } else if (a) {
        alice.push_back(tt);
        aliceMp[tt].push_back(i + 1);
      } else if (b) {
        bob.push_back(tt);
        bobMp[tt].push_back(i + 1);
      } else {
        other.push_back(tt);
        OtherMp[tt].push_back(i + 1);
      }
    }
    sort((both).begin(), (both).end());
    sort((alice).begin(), (alice).end());
    sort((bob).begin(), (bob).end());
    sort((other).begin(), (other).end());
    reverse((alice).begin(), (alice).end());
    reverse((bob).begin(), (bob).end());
    int ans = solve(both, alice, bob, other, m, k);
    ;
    ;
    cout << ans << "\n";
    if (ans != -1) {
      solve2(both, alice, bob, other, m, k, ans);
    }
  }
}
