#include <bits/stdc++.h>
using namespace std;
template <class T>
void Output(const vector<T>& v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << " ";
    else
      cout << endl;
  }
}
template <class T>
void Input(vector<T>& v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}
template <class T>
vector<int> Query(const T& c, int answer_counts) {
  cout << "? " << c.size();
  for (auto it = c.begin(); it != c.end(); ++it) {
    cout << " " << (*it);
  }
  cout << endl;
  cout.flush();
  vector<int> s(answer_counts);
  for (int i = 0; i < answer_counts; ++i) {
    cin >> s[i];
  }
  return s;
}
inline int get(int k, const vector<int>& v) {
  if (k <= 0) return 0;
  return v[k - 1];
}
struct Book {
  int t, a, b, id, rank;
};
struct compare {
  bool operator()(const Book& a, const Book& b) { return a.t < b.t; }
};
int init(const vector<Book>& books, const vector<int>& a_like_index,
         const vector<int>& b_like_index, const vector<int>& both_like_index,
         const vector<int>& no_like_index, int both, int m, int k,
         set<Book, compare>& s) {
  for (int i = both; i < both_like_index.size(); ++i) {
    s.insert(books[both_like_index[i]]);
  }
  for (int i = k - both; i < a_like_index.size(); ++i) {
    s.insert(books[a_like_index[i]]);
  }
  for (int i = k - both; i < b_like_index.size(); ++i) {
    s.insert(books[b_like_index[i]]);
  }
  for (int i = 0; i < no_like_index.size(); ++i) {
    s.insert(books[no_like_index[i]]);
  }
  while (s.size() > m - both - (k - both) * 2) {
    auto it = s.end();
    --it;
    s.erase(it);
  }
  int ans = 0;
  for (const auto& b : s) {
    ans += b.t;
  }
  return ans;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<Book> books(n);
  vector<int> a_like;
  vector<int> b_like;
  vector<int> both_like;
  vector<int> no_like;
  vector<int> a_like_index;
  vector<int> b_like_index;
  vector<int> both_like_index;
  vector<int> no_like_index;
  for (int i = 0; i < n; ++i) {
    cin >> books[i].t >> books[i].a >> books[i].b;
    books[i].id = i + 1;
  }
  sort(books.begin(), books.end(),
       [](const Book& a, const Book& b) { return a.t < b.t; });
  for (int i = 0; i < n; ++i) {
    if (books[i].a == 1 && books[i].b == 1) {
      books[i].rank = both_like.size();
      both_like.push_back(books[i].t +
                          (both_like.size() > 0 ? both_like.back() : 0));
      both_like_index.push_back(i);
    }
    if (books[i].a == 1 && books[i].b == 0) {
      books[i].rank = a_like.size();
      a_like.push_back(books[i].t + (a_like.size() > 0 ? a_like.back() : 0));
      a_like_index.push_back(i);
    }
    if (books[i].a == 0 && books[i].b == 1) {
      books[i].rank = b_like.size();
      b_like.push_back(books[i].t + (b_like.size() > 0 ? b_like.back() : 0));
      b_like_index.push_back(i);
    }
    if (books[i].a == 0 && books[i].b == 0) {
      books[i].rank = no_like.size();
      no_like.push_back(books[i].t + (no_like.size() > 0 ? no_like.back() : 0));
      no_like_index.push_back(i);
    }
  }
  int best = 0x7FFFFFFF;
  int best_i = -1;
  int rest_sum = 0;
  set<Book, compare> rest;
  bool inited = false;
  for (int i = 0; i <= both_like.size() && i <= k; ++i) {
    if (k - i > a_like.size() || k - i > b_like.size() ||
        m - i - (k - i) * 2 < 0) {
      continue;
    }
    int t = get(i, both_like) + get(k - i, a_like) + get(k - i, b_like);
    if (!inited) {
      rest_sum = init(books, a_like_index, b_like_index, both_like_index,
                      no_like_index, i, m, k, rest);
      t += rest_sum;
      inited = true;
    } else {
      while (rest.size() > m - i - (k - i) * 2) {
        auto it = rest.end();
        --it;
        rest_sum -= it->t;
        rest.erase(it);
      }
      t += rest_sum;
    }
    if (t < best) {
      best = t;
      best_i = i;
    }
    if (k - i > 0) {
      rest_sum += books[a_like_index[k - i - 1]].t;
      rest_sum += books[b_like_index[k - i - 1]].t;
      rest.insert(books[a_like_index[k - i - 1]]);
      rest.insert(books[b_like_index[k - i - 1]]);
    }
    if (i < both_like_index.size()) {
      rest_sum -= books[both_like_index[i]].t;
      rest.erase(books[both_like_index[i]]);
    }
  }
  if (best_i == -1) {
    cout << -1 << endl;
    return 0;
  }
  cout << best << endl;
  rest.clear();
  init(books, a_like_index, b_like_index, both_like_index, no_like_index,
       best_i, m, k, rest);
  for (int i = 0; i < best_i; ++i) {
    cout << books[both_like_index[i]].id << " ";
  }
  for (int i = 0; i < k - best_i; ++i) {
    cout << books[a_like_index[i]].id << " ";
  }
  for (int i = 0; i < k - best_i; ++i) {
    cout << books[b_like_index[i]].id << " ";
  }
  for (const auto& b : rest) {
    cout << b.id << " ";
  }
  cout << endl;
  return 0;
}
