#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
  stringstream s;
  s << "[";
  for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T* val) {
  cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __evars_out_var(a);
  cerr << "; ";
  __evars(++it, args...);
}
int const LIM = 2e6 + 3;
int const N = 1e6;
int arr[LIM + 3];
long long inf = 1e18;
int par[LIM + 3];
map<int, long long> to;
int main() {
  int tc;
  tc = 1;
  while (tc--) {
    std::vector<pair<int, int> > A, B, C, D;
    vector<int> par_A, par_B, par_C;
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
      int x, a, b;
      scanf("%d %d %d", &x, &a, &b);
      if (a + b == 2) {
        C.push_back({x, i});
      } else if (a == 1 && b == 0) {
        B.push_back({x, i});
      } else if (a == 0 && b == 1) {
        A.push_back({x, i});
      } else
        D.push_back({x, i});
    }
    sort(B.begin(), B.end());
    sort(A.begin(), A.end());
    sort(C.begin(), C.end());
    if (!A.empty()) par_A.push_back(A[0].first);
    for (int i = 1; i < A.size(); i++)
      par_A.push_back(par_A.back() + A[i].first);
    if (!B.empty()) par_B.push_back(B[0].first);
    for (int i = 1; i < B.size(); i++)
      par_B.push_back(par_B.back() + B[i].first);
    if (!C.empty()) par_C.push_back(C[0].first);
    for (int i = 1; i < C.size(); i++)
      par_C.push_back(par_C.back() + C[i].first);
    int sz1 = A.size(), sz2 = B.size();
    if (C.size() + min(sz1, sz2) < k)
      puts("-1");
    else {
      D.push_back({1e9 + 1, n + 1});
      A.push_back({1e9 + 1, n + 1});
      B.push_back({1e9 + 1, n + 1});
      C.push_back({1e9 + 1, n + 1});
      int tst = 0, l = 0, r = 0;
      for (int i = 0; i < k; i++)
        if (C[l].first < B[r].first + A[r].first)
          tst += C[l++].first;
        else
          tst += B[r].first + A[r].first, r++;
      if (l + r * 2 <= m / 2 * 2) {
        int tmp = l + r * 2;
        std::vector<int> out;
        int a = r, b = r, c = 0;
        while (tmp < m) {
          int mn = min(A[a].first, min(B[b].first, D[c].first));
          if (A[a].first == mn)
            out.push_back(A[a].second), tst += A[a++].first;
          else if (B[b].first == mn)
            out.push_back(B[b].second), tst += B[b++].first;
          else
            out.push_back(D[c].second), tst += D[c++].first;
          tmp++;
        }
        cout << tst << endl;
        for (auto x : out) printf("%d ", x);
        for (int j = 0; j < l; j++) printf("%d ", C[j].second);
        for (int j = 0; j < r; j++) printf("%d %d ", A[j].second, B[j].second);
        puts("");
        return 0;
      }
      int sz = C.size();
      int Ans = 2e9 + 1, ind = -1;
      for (int i = 0; i < min(m, sz); i++) {
        if ((k - i) * 2 > (m - i)) continue;
        if ((k - i) > min(sz1, sz2)) continue;
        if ((m - i) % 2 == 0) {
          if (min(sz1, sz2) * 2 < (m - i))
            continue;
          else if ((i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
                       par_B[k - i - 1] <
                   Ans)
            Ans = min(Ans, (i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
                               par_B[k - i - 1]),
            ind = i;
        } else {
          if (sz1 == sz2 && min(sz1, sz2) * 2 < (m - i))
            continue;
          else if (Ans >
                   (i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
                       par_B[k - i - 1] +
                       min(A[k - i].first, min(B[k - i].first, D[0].first)))
            Ans = min(Ans,
                      (i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
                          par_B[k - i - 1] +
                          min(A[k - i].first, min(B[k - i].first, D[0].first))),
            ind = i;
        }
      }
      cout << Ans << endl;
      int i = ind;
      for (int j = 0; j < i; j++) printf("%d ", C[j].second);
      for (int j = 0; j < (k - i); j++)
        printf("%d %d ", A[j].second, B[j].second);
      if ((m - i) % 2 != 0) {
        int mn = min(A[k - i].first, min(B[k - i].first, D[0].first));
        if (A[k - i].first == mn)
          printf("%d ", A[k - i].second);
        else if (B[k - i].first == mn)
          printf("%d ", B[k - i].second);
        else
          printf("%d ", D[0].second);
      }
      puts("");
    }
  }
}
