#include <bits/stdc++.h>
using namespace std;
struct Book {
  int i;
  long long t = 0;
  int a = 0;
  int b = 0;
  Book() {}
  Book(int i, long long t, int a, int b) {
    this->i = i;
    this->t = t;
    this->a = a;
    this->b = b;
  }
};
struct Result {
  long long sum = -1;
  vector<int> chosenBooks{};
  Result() {}
  Result(long long a, vector<int> b) {
    this->sum = a;
    this->chosenBooks = b;
  }
  Result(const Result& r) {
    this->sum = r.sum;
    this->chosenBooks = r.chosenBooks;
  }
};
static int calc_t(const vector<int>& xs, const vector<Book>& books) {
  int t = 0;
  for (auto x : xs) t += books[x].t;
  return t;
}
static int calc_t(const set<int>& xs, const vector<Book>& books) {
  int t = 0;
  for (auto x : xs) t += books[x].t;
  return t;
}
static string to_key(const vector<int>& ys) {
  vector<int> xs = ys;
  sort(xs.begin(), xs.end(), [](const int& a, const int& b) { return a < b; });
  string s = "";
  for (auto x : xs) {
    s += to_string(x) + "@";
  }
  return s;
}
static Result f(int likes, int b2i, int babi, vector<int>* chosenBooks,
                map<string, Result>* result_lookup, const int min_likes,
                const int num_books, const vector<int>& b2,
                const vector<vector<int>>& bab, const vector<Book>& books) {
  if (result_lookup->find(to_key(*chosenBooks)) != result_lookup->end()) {
    return Result((*result_lookup)[to_key(*chosenBooks)]);
  }
  if (likes < min_likes && chosenBooks->size() < num_books) {
    if (babi < bab.size() && chosenBooks->size() + 2 <= num_books &&
        b2i < b2.size()) {
      auto c1 = books[b2[b2i]].t;
      auto c2 = calc_t(bab[babi], books);
      if (0 && likes >= min_likes - 2) {
        auto ss1 = *chosenBooks;
        ss1.push_back(b2[b2i]);
        Result r1 = f(likes + 1, b2i + 1, babi, &ss1, result_lookup, min_likes,
                      num_books, b2, bab, books);
        (*result_lookup)[to_key(ss1)] = r1;
        auto ss2 = *chosenBooks;
        for (auto x : bab[babi]) {
          ss2.push_back(x);
        }
        Result r2 = f(likes + 1, b2i, babi + 1, &ss2, result_lookup, min_likes,
                      num_books, b2, bab, books);
        (*result_lookup)[to_key(ss2)] = r2;
        if (r1.sum == -1) return r2;
        if (r2.sum == -1) return r1;
        return r1.sum < r2.sum ? r1 : r2;
      } else {
        if (c2 < c1) {
          for (auto x : bab[babi]) {
            chosenBooks->push_back(x);
          }
          auto r1 = f(likes + 1, b2i, babi + 1, chosenBooks, result_lookup,
                      min_likes, num_books, b2, bab, books);
          (*result_lookup)[to_key(*chosenBooks)] = r1;
          return r1;
        } else {
          chosenBooks->push_back(b2[b2i]);
          auto r1 = f(likes + 1, b2i + 1, babi, chosenBooks, result_lookup,
                      min_likes, num_books, b2, bab, books);
          (*result_lookup)[to_key(*chosenBooks)] = r1;
          return r1;
        }
      }
    } else if (babi < bab.size() && chosenBooks->size() + 2 <= num_books) {
      for (auto x : bab[babi]) {
        chosenBooks->push_back(x);
      }
      auto r1 = f(likes + 1, b2i, babi + 1, chosenBooks, result_lookup,
                  min_likes, num_books, b2, bab, books);
      (*result_lookup)[to_key(*chosenBooks)] = r1;
      return r1;
    } else if (b2i < b2.size()) {
      chosenBooks->push_back(b2[b2i]);
      auto r1 = f(likes + 1, b2i + 1, babi, chosenBooks, result_lookup,
                  min_likes, num_books, b2, bab, books);
      (*result_lookup)[to_key(*chosenBooks)] = r1;
      return r1;
    } else {
      return Result(-1, vector<int>{});
    }
  } else {
    vector<int> ss = *chosenBooks;
    long long sum = calc_t(ss, books);
    int num_pad = num_books - ss.size();
    if (num_pad > 0) {
      for (int i = 0; i < books.size(); i++) {
        if (find(ss.begin(), ss.end(), i) != ss.end()) continue;
        sum += books[i].t;
        ss.push_back(i);
        num_pad--;
        if (!num_pad) break;
      }
    }
    return Result(sum, ss);
  }
}
int main() {
  int n = 0;
  int min_likes = 0;
  int num_books = 0;
  cin >> n >> num_books >> min_likes;
  if (n < num_books || num_books < min_likes) {
    cout << -1 << endl;
    return 0;
  }
  int a_count = 0, b_count = 0;
  vector<Book> books;
  for (int i = 0; i < n; i++) {
    long long t = 0;
    int a = 0, b = 0;
    cin >> t >> a >> b;
    a_count += a;
    b_count += b;
    books.push_back(Book(i + 1, t, a, b));
  }
  if (a_count < min_likes || b_count < min_likes) {
    cout << -1 << endl;
    return 0;
  }
  sort(books.begin(), books.end(),
       [](const Book& a, const Book& b) { return a.t < b.t; });
  vector<int> b2;
  vector<vector<int>> bab;
  {
    vector<int> ba, bb;
    for (int i = 0; i < n; i++) {
      auto bk = books[i];
      if (bk.a && bk.b) {
        b2.push_back(i);
      } else if (bk.a) {
        ba.push_back(i);
      } else if (bk.b) {
        bb.push_back(i);
      }
    }
    int nn = ba.size() < bb.size() ? ba.size() : bb.size();
    for (int i = 0; i < nn; i++) {
      bab.push_back(vector<int>{ba[i], bb[i]});
    }
  }
  vector<int> ss{};
  map<string, Result> result_lookup{};
  Result r =
      f(0, 0, 0, &ss, &result_lookup, min_likes, num_books, b2, bab, books);
  auto chosenBooks = r.chosenBooks;
  auto sum = r.sum;
  if (sum < 0) {
    cout << -1 << endl;
    return 0;
  }
  cout << sum << endl;
  auto p = chosenBooks.begin();
  for (int i = 0; i < chosenBooks.size(); i++) {
    if (i > 0) cout << " ";
    cout << books[*p].i;
    advance(p, 1);
  }
  cout << endl;
  return 0;
}
