#include <bits/stdc++.h>
using namespace std;
struct et {
  int val;
  int index;
  et(int val, int index) {
    this->val = val;
    this->index = index;
  }
  bool operator<(const et& other) const {
    if (this->val == other.val) {
      return this->index < other.index;
    }
    return this->val < other.val;
  }
};
int main() {
  iostream::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<et> groups[4];
  for (int i = 0; i < n; i++) {
    int v, a, b;
    cin >> v >> a >> b;
    groups[a * 2 + b].push_back(et(v, i));
  }
  int sz[4];
  vector<int> prefix[4];
  for (int i = 0; i < 4; i++) {
    sort(groups[i].begin(), groups[i].end());
    sz[i] = groups[i].size();
    prefix[i].push_back(0);
    for (et x : groups[i]) {
      prefix[i].push_back(prefix[i].back() + x.val);
    }
  }
  int start = -1;
  for (int cnt = 0; cnt <= sz[3] && cnt <= m; cnt++) {
    int two = k - cnt;
    if (two > 0) {
      if (2 * two + cnt <= m && n - sz[3] + cnt >= m &&
          two <= min(sz[1], sz[2])) {
        start = cnt;
        break;
      }
    } else {
      if (n - sz[3] + cnt >= m) {
        start = cnt;
        break;
      }
    }
  }
  if (start == -1) {
    cout << -1 << endl;
    return 0;
  }
  int minanswer = 2000000010;
  int minboy = -1;
  set<et> selected;
  set<et> free;
  for (int i = 0; i < 3; i++) {
    for (et x : groups[i]) {
      free.insert(x);
    }
  }
  for (int i = 0; i < (k - start); i++) {
    free.erase(groups[1][i]);
    free.erase(groups[2][i]);
  }
  int sumselected = 0;
  for (int i = 0; i < (m - (start + max(0, k - start) * 2)); i++) {
    et x = *free.begin();
    free.erase(x);
    selected.insert(x);
    sumselected += x.val;
  }
  for (int cnt = start; cnt <= sz[3] && cnt <= m; cnt++) {
    int two = max(0, k - cnt);
    int curanswer =
        prefix[3][cnt] + sumselected + prefix[2][two] + prefix[1][two];
    if (curanswer < minanswer) {
      minanswer = curanswer;
      minboy = cnt;
    }
    if (cnt == sz[3] || cnt == m) break;
    if (k - cnt > 0) {
      int id = k - cnt - 1;
      free.insert(groups[1][id]);
      free.insert(groups[2][id]);
    }
    int selectreq = m - (cnt + 2 * max(0, k - cnt));
    bool cont = true;
    while (cont) {
      cont = false;
      if (selectreq > selected.size()) {
        cont = true;
        sumselected += (*free.begin()).val;
        selected.insert(*free.begin());
        free.erase(free.begin());
      } else if (selectreq < selected.size()) {
        cont = true;
        sumselected -= (*selected.rbegin()).val;
        selected.erase(*(selected.rbegin()));
      } else if (free.size() > 0 && selected.size() > 0 &&
                 (*selected.rbegin()).val > (*free.begin()).val) {
        cont = true;
        sumselected -= (*selected.rbegin()).val;
        selected.erase(*selected.rbegin());
        sumselected += (*free.begin()).val;
        selected.insert(*free.begin());
        free.erase(free.begin());
      }
    }
  }
  selected.clear();
  free.clear();
  sumselected = 0;
  for (int i = 0; i < 3; i++) {
    for (et x : groups[i]) {
      free.insert(x);
    }
  }
  for (int i = 0; i < (k - start); i++) {
    free.erase(groups[1][i]);
    free.erase(groups[2][i]);
  }
  for (int i = 0; i < (m - (start + max(0, k - start) * 2)); i++) {
    et x = *free.begin();
    free.erase(x);
    selected.insert(x);
    sumselected += x.val;
  }
  for (int cnt = start; cnt <= minboy; cnt++) {
    if (k - cnt > 0) {
      int id = k - cnt - 1;
      free.insert(groups[1][id]);
      free.insert(groups[2][id]);
    }
    if (cnt == minboy) break;
    int selectreq = m - (cnt + 2 * max(0, k - cnt));
    bool cont = true;
    while (cont) {
      cont = false;
      if (selectreq > selected.size()) {
        cont = true;
        sumselected += (*free.begin()).val;
        selected.insert(*free.begin());
        free.erase(free.begin());
      } else if (selectreq < selected.size()) {
        cont = true;
        sumselected -= (*selected.rbegin()).val;
        selected.erase(*(selected.rbegin()));
      } else if (free.size() > 0 && selected.size() > 0 &&
                 (*selected.rbegin()).val > (*free.begin()).val) {
        cont = true;
        sumselected -= (*selected.rbegin()).val;
        selected.erase(*selected.rbegin());
        sumselected += (*free.begin()).val;
        selected.insert(*free.begin());
        free.erase(free.begin());
      }
    }
  }
  cout << minanswer << endl;
  for (et x : selected) {
    cout << x.index + 1 << ' ';
  }
  for (int i = 0; i < minboy; i++) {
    cout << groups[3][i].index + 1 << ' ';
  }
  for (int i = 0; i < max(0, k - minboy); i++) {
    cout << groups[2][i].index + 1 << ' ';
    cout << groups[1][i].index + 1 << ' ';
  }
  cout << endl;
  return 0;
}
