#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int DEBUG = 0;
  int n, m, k, t, _a, _b;
  cin >> n >> m >> k;
  vector<int> ab, a, b, nf;
  vector<pair<int, int>> ab_index, a_index, b_index, nf_index;
  for (int i = 0; i < n; i++) {
    cin >> t >> _a >> _b;
    if (_a && _b)
      ab_index.push_back({t, i + 1});
    else if (_a && !_b)
      a_index.push_back({t, i + 1});
    else if (!_a && _b)
      b_index.push_back({t, i + 1});
    else
      nf_index.push_back({t, i + 1});
  }
  sort(ab_index.begin(), ab_index.end());
  sort(a_index.begin(), a_index.end());
  sort(b_index.begin(), b_index.end());
  sort(nf_index.begin(), nf_index.end());
  for (auto _ : ab_index) ab.push_back(_.first);
  for (auto _ : a_index) a.push_back(_.first);
  for (auto _ : b_index) b.push_back(_.first);
  for (auto _ : nf_index) nf.push_back(_.first);
  vector<int>::iterator nfI = nf.begin(), abI = ab.begin(), aI = a.begin(),
                        bI = b.begin();
  int T = 0;
  while ((nfI != ab.end() || abI != ab.end() || aI < a.end() || bI < b.end()) &&
         (nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI -
          b.begin()) < m) {
    if (DEBUG) cerr << "nf used: " << nfI - nf.begin() << endl;
    if (DEBUG) cerr << "ab used: " << abI - ab.begin() << endl;
    if (DEBUG) cerr << "a used: " << aI - a.begin() << endl;
    if (DEBUG) cerr << "b used: " << bI - b.begin() << endl;
    if (DEBUG)
      cerr << "R: "
           << (nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI -
               b.begin())
           << endl;
    if (DEBUG)
      cerr << "L: " << (abI - ab.begin() + min(aI - a.begin(), bI - b.begin()))
           << endl;
    int nf_t = (nfI != nf.end() ? *nfI : numeric_limits<int>::max());
    int ab_t = (abI != ab.end() ? *abI : numeric_limits<int>::max());
    int a_t = (aI != a.end() ? *aI : numeric_limits<int>::max());
    int b_t = (bI != b.end() ? *bI : numeric_limits<int>::max());
    if (ab_t != numeric_limits<int>::max() && ab_t <= nf_t && ab_t <= a_t &&
        ab_t <= b_t) {
      if (DEBUG) cerr << "use ab book " << *abI << "\n";
      T += *abI++;
      continue;
    } else if (a_t != numeric_limits<int>::max() && a_t <= nf_t &&
               a_t <= ab_t && a_t <= b_t) {
      if (DEBUG) cerr << "use a book " << *aI << "\n";
      T += *aI++;
      continue;
    } else if (b_t != numeric_limits<int>::max() && b_t <= nf_t &&
               b_t <= ab_t && b_t <= a_t) {
      if (DEBUG) cerr << "use b book " << *bI << "\n";
      T += *bI++;
      continue;
    } else if (nf_t != numeric_limits<int>::max() && nf_t <= a_t &&
               nf_t <= b_t && nf_t <= ab_t) {
      if (DEBUG) cerr << "use nf book " << *nfI << "\n";
      T += *nfI++;
      continue;
    } else {
      if (DEBUG) cerr << "Out of books!\n";
      break;
    }
  }
  if ((nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI - b.begin()) !=
      m) {
    cout << -1 << endl;
    return 0;
  }
  while ((abI - ab.begin() + min(aI - a.begin(), bI - b.begin())) < k) {
    int ab_rep_nf =
        ((abI != ab.end() && nfI != nf.begin()) ? *abI - *(nfI - 1)
                                                : numeric_limits<int>::max());
    int a_rep_nf = ((aI != a.end() && nfI != nf.begin() &&
                     (bI - b.begin() > aI - a.begin()))
                        ? *aI - *(nfI - 1)
                        : numeric_limits<int>::max());
    int b_rep_nf = ((bI != b.end() && nfI != nf.begin() &&
                     (aI - a.begin() > bI - b.begin()))
                        ? *bI - *(nfI - 1)
                        : numeric_limits<int>::max());
    int pair_rep_nf = ((aI != a.end() && bI != b.end() && nfI - nf.begin() > 1)
                           ? (*aI + *bI) - *(nfI - 1) - *(nfI - 2)
                           : numeric_limits<int>::max());
    int ab_rep_pair =
        ((ab.end() - abI > 1 && aI != a.begin() && bI != b.begin())
             ? *abI + *(abI + 1) - *(aI - 1) - *(bI - 1)
             : numeric_limits<int>::max());
    int ab_rep_a = ((abI != ab.end() && aI - a.begin() > bI - b.begin())
                        ? *abI - *(aI - 1)
                        : numeric_limits<int>::max());
    int ab_rep_b = ((abI != ab.end() && bI - b.begin() > aI - a.begin())
                        ? *abI - *(bI - 1)
                        : numeric_limits<int>::max());
    int a_rep_b =
        ((aI != a.end() && int(bI - b.begin()) - int(aI - a.begin()) > 1)
             ? *aI - *(bI - 1)
             : numeric_limits<int>::max());
    int b_rep_a =
        ((bI != b.end() && int(aI - a.begin()) - int(bI - b.begin()) > 1)
             ? *bI - *(aI - 1)
             : numeric_limits<int>::max());
    if (ab_rep_nf != numeric_limits<int>::max() &&
        (ab_rep_nf <= a_rep_nf && ab_rep_nf <= b_rep_nf &&
         ab_rep_nf <= pair_rep_nf && ab_rep_nf <= ab_rep_pair &&
         ab_rep_nf <= ab_rep_a && ab_rep_nf <= ab_rep_b &&
         ab_rep_nf <= a_rep_b && ab_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with ab " << *abI << endl;
      T -= *nfI;
      T += *abI++;
    } else if (a_rep_nf != numeric_limits<int>::max() &&
               (a_rep_nf <= ab_rep_nf && a_rep_nf <= b_rep_nf &&
                a_rep_nf <= pair_rep_nf && a_rep_nf <= ab_rep_pair &&
                a_rep_nf <= ab_rep_a && a_rep_nf <= ab_rep_b &&
                a_rep_nf <= a_rep_b && a_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with a " << *aI << endl;
      T -= *nfI;
      T += *aI++;
    } else if (b_rep_nf != numeric_limits<int>::max() &&
               (b_rep_nf <= ab_rep_nf && b_rep_nf <= a_rep_nf &&
                b_rep_nf <= pair_rep_nf && b_rep_nf <= ab_rep_pair &&
                b_rep_nf <= ab_rep_a && b_rep_nf <= ab_rep_b &&
                b_rep_nf <= a_rep_b && b_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with b " << *aI << endl;
      T -= *nfI;
      T += *bI++;
    } else if (pair_rep_nf != numeric_limits<int>::max() &&
               (pair_rep_nf <= ab_rep_nf && pair_rep_nf <= a_rep_nf &&
                pair_rep_nf <= b_rep_nf && pair_rep_nf <= ab_rep_pair &&
                pair_rep_nf <= ab_rep_a && pair_rep_nf <= ab_rep_b &&
                pair_rep_nf <= a_rep_b && pair_rep_nf <= b_rep_a)) {
      nfI -= 2;
      if (DEBUG)
        cerr << "Replace nf books (" << *(nfI + 1) << " + " << *(nfI + 2)
             << ") with pair (" << *aI << " + " << *bI << ")" << endl;
      T -= *nfI;
      T -= *(nfI + 1);
      T += *aI++;
      T += *bI++;
    } else if (ab_rep_pair != numeric_limits<int>::max() &&
               (ab_rep_pair <= ab_rep_nf && ab_rep_pair <= a_rep_nf &&
                ab_rep_pair <= b_rep_nf && ab_rep_pair <= pair_rep_nf &&
                ab_rep_pair <= ab_rep_a && ab_rep_pair <= ab_rep_b &&
                ab_rep_pair <= a_rep_b && ab_rep_pair <= b_rep_a)) {
      aI--;
      bI--;
      if (DEBUG)
        cerr << "Replace pair books (" << *aI << " + " << *bI << ") with ab ("
             << *abI << " + " << *(abI + 1) << ")" << endl;
      T -= *aI;
      T -= *bI;
      T += *abI++;
      T += *abI++;
    } else if (ab_rep_a != numeric_limits<int>::max() &&
               (ab_rep_a <= ab_rep_nf && ab_rep_a <= a_rep_nf &&
                ab_rep_a <= b_rep_nf && ab_rep_a <= pair_rep_nf &&
                ab_rep_a <= ab_rep_pair && ab_rep_a <= ab_rep_b &&
                ab_rep_a <= a_rep_b && ab_rep_a <= b_rep_a)) {
      aI--;
      if (DEBUG)
        cerr << "Replace a book " << *aI << " with ab " << *abI << endl;
      T -= *aI;
      T += *abI++;
    } else if (ab_rep_b != numeric_limits<int>::max() &&
               (ab_rep_b <= ab_rep_nf && ab_rep_b <= a_rep_nf &&
                ab_rep_b <= b_rep_nf && ab_rep_b <= pair_rep_nf &&
                ab_rep_b <= ab_rep_pair && ab_rep_b <= ab_rep_a &&
                ab_rep_b <= a_rep_b && ab_rep_b <= b_rep_a)) {
      bI--;
      if (DEBUG)
        cerr << "Replace b book " << *bI << " with ab " << *abI << endl;
      T -= *bI;
      T += *abI++;
    } else if (a_rep_b != numeric_limits<int>::max() &&
               (a_rep_b <= ab_rep_nf && a_rep_b <= a_rep_nf &&
                a_rep_b <= b_rep_nf && a_rep_b <= pair_rep_nf &&
                a_rep_b <= ab_rep_pair && a_rep_b <= ab_rep_a &&
                a_rep_b <= ab_rep_b && a_rep_b <= b_rep_a)) {
      bI--;
      if (DEBUG) cerr << "Replace b book " << *bI << " with a " << *aI << endl;
      T -= *bI;
      T += *aI++;
    } else if (b_rep_a != numeric_limits<int>::max() &&
               (b_rep_a <= ab_rep_nf && b_rep_a <= a_rep_nf &&
                b_rep_a <= b_rep_nf && b_rep_a <= pair_rep_nf &&
                b_rep_a <= ab_rep_pair && b_rep_a <= ab_rep_a &&
                b_rep_a <= ab_rep_b && b_rep_a <= a_rep_b)) {
      aI--;
      if (DEBUG) cerr << "Replace a book " << *aI << " with b " << *aI << endl;
      T -= *aI;
      T += *bI++;
    } else {
      if (DEBUG) cerr << "No more books to replace!\n";
      break;
    }
    if (DEBUG)
      cerr << "L: " << (abI - ab.begin() + min(aI - a.begin(), bI - b.begin()))
           << endl;
  }
  if ((abI - ab.begin() + min(aI - a.begin(), bI - b.begin())) < k) {
    cout << -1 << endl;
    return 0;
  }
  cout << T << endl;
  for (vector<int>::iterator VI = ab.begin(); VI < abI; VI++)
    cout << ab_index[VI - ab.begin()].second << " ";
  for (vector<int>::iterator VI = nf.begin(); VI < nfI; VI++)
    cout << nf_index[VI - nf.begin()].second << " ";
  for (vector<int>::iterator VI = a.begin(); VI < aI; VI++)
    cout << a_index[VI - a.begin()].second << " ";
  for (vector<int>::iterator VI = b.begin(); VI < bI; VI++)
    cout << b_index[VI - b.begin()].second << " ";
  cout << endl;
  return 0;
}
