#include <bits/stdc++.h>
using namespace std;
enum Book { Common = 0, A = 1, B = 2, AB = 3, Junk = 4 };
void log(Book book) {
  return;
  switch (book) {
    case 0:
      cout << "Taking a common book\n";
      return;
    case 1:
      cout << "Taking separate A book\n";
      return;
    case 2:
      cout << "Taking separate B book\n";
      return;
    case 3:
      cout << "Taking 2 separate books\n";
      return;
    case 4:
      cout << "Taking a book for space\n";
      return;
  }
}
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a = 0, b = 0;
  int books = 0;
  int answer[n];
  int answersize = 0;
  multiset<pair<int, int>> commons, as, bs, junk;
  long long time = 0;
  for (int i = 1; i <= n; i++) {
    int t0;
    bool likesa, likesb;
    cin >> t0 >> likesa >> likesb;
    pair<int, int> t = {t0, i};
    if (likesa)
      if (likesb)
        commons.insert(t);
      else
        as.insert(t);
    else if (likesb)
      bs.insert(t);
    else
      junk.insert(t);
  }
  if (as.size() + commons.size() < k || bs.size() + commons.size() < k) {
    cout << -1 << endl;
    return;
  }
  auto ita = as.begin();
  auto itb = bs.begin();
  auto itc = commons.begin();
  while (books < m && a < k && itc != commons.end() && ita != as.end() &&
         itb != bs.end()) {
    int t1 = ita->first + itb->first, t2 = itc->first;
    int cantake = m - books;
    int needs = k - a;
    if (cantake == needs || cantake == 1 || t1 > t2) {
      time += t2;
      books++;
      answer[answersize++] = itc->second;
      itc++;
      log(Book::Common);
    } else {
      answer[answersize++] = ita->second;
      answer[answersize++] = itb->second;
      ita++;
      itb++;
      log(Book::AB);
      time += t1;
      books += 2;
    }
    a++;
    b++;
  }
  if (books == m || a == k) {
  } else if (itc == commons.end()) {
    for (; a < k; a++, ita++, books++) {
      time += ita->first;
      answer[answersize++] = ita->second;
      log(Book::A);
    }
    for (; b < k; b++, itb++, books++) {
      time += itb->first;
      answer[answersize++] = itb->second;
      log(Book::B);
    }
  } else if (ita == as.end()) {
    for (; a < k; a++, b++, itc++, books++) {
      time += itc->first;
      answer[answersize++] = itc->second;
      log(Book::Common);
    }
    for (; b < k; b++, itb++, books++) {
      time += itb->first;
      answer[answersize++] = itb->second;
      log(Book::B);
    }
  } else {
    for (; b < k; a++, b++, itc++, books++) {
      time += itc->first;
      answer[answersize++] = itc->second;
      log(Book::Common);
    }
    for (; a < k; a++, ita++, books++) {
      time += ita->first;
      answer[answersize++] = ita->second;
      log(Book::A);
    }
  }
  auto itj = junk.begin();
  int big = 10001;
  for (; books < m; books++) {
    int ta = ita == as.end() ? big : ita->first,
        tb = itb == bs.end() ? big : itb->first;
    int tc = itc == commons.end() ? big : itc->first,
        tj = itj == junk.end() ? big : itj->first;
    int all[] = {ta, tb, tc, tj};
    sort(all, all + 4);
    int small = all[0];
    time += small;
    if (small == ta) {
      answer[answersize++] = ita->second;
      ita++;
    } else if (small == tb) {
      answer[answersize++] = itb->second;
      itb++;
    } else if (small == tc) {
      answer[answersize++] = itc->second;
      itc++;
    } else if (small == tj) {
      answer[answersize++] = itj->second;
      itj++;
    }
    log(Book::Junk);
  }
  cout << time << endl;
  for (int i = 0; i < answersize; i++) cout << answer[i] << " ";
}
int main() {
  ios_base::sync_with_stdio(0);
  solve();
}
