#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
long long TESTCASE_NUM = 1;
clock_t CLOCK_START_TIME = clock();
template <typename T>
inline void _read(T &x) {
  cin >> x;
}
inline void _read(int &x) { scanf("%d", &x); }
inline void _read(long long &x) { scanf("%lld", &x); }
inline void _read(float &x) { scanf("%f", &x); }
inline void _read(double &x) { scanf("%lf", &x); }
inline void _read(char &x) { x = getchar(); }
inline void _read(char *x) { scanf("%s", x); }
inline void read() {}
template <typename T, typename... U>
inline void read(T &head, U &...tail) {
  _read(head);
  read(tail...);
}
template <typename T>
inline void _write(const T &x) {
  cout << x;
}
inline void _write(const int &x) { printf("%d", x); }
inline void _write(const long long &x) { printf("%lld", x); }
inline void _write(const float &x) { printf("%.6f", x); }
inline void _write(const double &x) { printf("%.8lf", x); }
inline void _write(const char &x) { putchar(x); }
inline void _write(const char *x) { printf("%s", x); }
inline void write() {}
template <typename T, typename... U>
inline void write(const T &head, const U &...tail) {
  _write(head);
  write(tail...);
}
inline void debug() {}
template <typename T, typename... U>
inline void debug(const T &head, const U &...tail) {}
void solve();
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
template <typename T>
inline bool isprime(T p) {
  if (p == 2 || p == 3) return 1;
  if (p < 2 || (p % 6 != 1 && p % 6 != 5)) return 0;
  for (T i = 5; i * i <= p; i = i + 6)
    if (p % i == 0 || p % (i + 2) == 0) return 0;
  return 1;
}
void DISPLAY_DEBUG_INFO() {}
signed main() {
  while (TESTCASE_NUM--) solve();
  DISPLAY_DEBUG_INFO();
  return 0;
}
vector<pair<long long, long long>> book[4];
long long a, b, t, n, m, k, ans, p[4], cur;
vector<long long> ansans;
pair<long long, long long> next(int x) { return book[x][p[x]]; }
long long time(int x) { return book[x][p[x]].first; }
long long time1(int x) { return book[x][p[x] + 1].first; }
long long name(int x) { return book[x][p[x]].second; }
bool ok(int x) { return book[x].size() > p[x]; }
bool ok1(int x) { return book[x].size() > (p[x] + 1); }
void add(int x) {
  debug("added ", name(x), '\n');
  ans += time(x);
  ansans.push_back(name(x));
  p[x]++;
}
void solve() {
  read(n, m, k);
  for (int i = 1; i <= n; i++) {
    read(t, a, b);
    pair<long long, long long> temp = {t, i};
    book[b * 2 + a].push_back(temp);
  }
  for (int i = 0; i <= 3; i++) sort(book[i].begin(), book[i].end());
  for (int i = 1; i <= m; i++) {
    if (cur < k) {
      if (ok(1) && ok(2) && ok(3)) {
        if (i != m && ok1(3) && time(1) + time(2) <= time(3) + time1(3)) {
          add(1), add(2);
          i++;
        } else if (i != m && (!ok1(3)) && time(1) + time(2) <= time(3) * 2) {
          add(1), add(2);
          i++;
        } else
          add(3);
      } else if (i != m && ok(1) && ok(2)) {
        add(1), add(2);
        i++;
      } else if (ok(3)) {
        add(3);
      } else {
        write(-1, '\n');
        return;
      }
      cur++;
    } else {
      vector<pair<pair<long long, long long>, int>> tt;
      for (int i = 0; i <= 3; i++) {
        if (ok(i)) {
          tt.push_back({next(i), i});
        }
      }
      if (tt.size() == 0) {
        write(-1, '\n');
        return;
      }
      sort(tt.begin(), tt.end());
      add(tt[0].second);
    }
  }
  if (cur < k) {
    write(-1, '\n');
    return;
  }
  write(ans, '\n');
  for (auto i : ansans) {
    write(i, ' ');
  }
  write('\n');
}
