#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const int mod = 1000000007;
const int mod2 = 998244353;
const double eps = 1e-9;
const double pi = acos(-1.0);
inline int rint() {
  int x;
  scanf("%d", &x);
  return x;
}
inline long long rll() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline long long ri64() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
void rvi(vector<int> &a, int n) {
  for (int i = 0; i < n; i++) {
    a.push_back(rint());
  }
}
void rvll(vector<long long> &a, int n) {
  for (int i = 0; i < n; i++) {
    a.push_back(rll());
  }
}
void showvi(vector<int> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%d%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
  }
}
void showvll(vector<long long> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%lld%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
  }
}
void showviln(vector<int> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%d%c", a[i], '\n');
  }
}
void showvllln(vector<long long> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%lld%c", a[i], '\n');
  }
}
void showi(int x) { printf("%d", x); }
void cfinit() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
using namespace std;
struct node {
  int t;
  int a;
  int b;
  int ind;
} ns[200022];
int n, m, k;
bool cmp(node &a, node &b) {
  if (a.t != b.t) return a.t < b.t;
  return a.ind < b.ind;
}
bool check(int x, bool show) {
  int n00 = 0;
  int n10 = 0;
  int n01 = 0;
  int n11 = 0;
  for (int i = 0; i < (x); i++) {
    if (ns[i].a && ns[i].b)
      n11++;
    else if (ns[i].a)
      n10++;
    else if (ns[i].b)
      n01++;
    else
      n00++;
  }
  if (n11 > m) return true;
  int fk = 0;
  int n0 = 0;
  int n1 = 0;
  fk += n11;
  n0 += n11;
  n1 += n11;
  if (n0 < k) {
    int rm = k - n0;
    if (n10 < rm) return false;
    fk += rm;
    n0 = k;
  }
  if (n1 < k) {
    int rm = k - n1;
    if (n01 < rm) return false;
    fk += rm;
    n1 = k;
  }
  if (fk <= m) return true;
  return false;
}
vector<int> n00;
vector<int> n10;
vector<int> n01;
vector<int> n11;
int pos00, pos10, pos01, pos11;
long long sum, now, mi;
int ans00, ans01, ans10, ans11;
int now0, now1;
void push() {
  int fk = -1;
  if (pos00 != n00.size()) fk = 0;
  if (pos01 != n01.size()) {
    if (fk == -1)
      fk = 1;
    else if (ns[n01[pos01]].t < ns[n00[pos00]].t)
      fk = 1;
  }
  if (pos10 != n10.size()) {
    if (fk == -1)
      fk = 2;
    else if (fk == 0 && ns[n10[pos10]].t < ns[n00[pos00]].t)
      fk = 2;
    else if (fk == 1 && ns[n10[pos10]].t < ns[n01[pos01]].t)
      fk = 2;
  }
  if (fk == 0) {
    sum += ns[n00[pos00]].t;
    pos00++;
  }
  if (fk == 1) {
    now1++;
    sum += ns[n01[pos01]].t;
    pos01++;
  }
  if (fk == 2) {
    now0++;
    sum += ns[n10[pos10]].t;
    pos10++;
  }
  now++;
}
int main() {
  cfinit();
  cin >> n >> m >> k;
  for (int i = 0; i < (n); i++) cin >> ns[i].t >> ns[i].a >> ns[i].b;
  for (int i = 0; i < (n); i++) ns[i].ind = i + 1;
  sort(ns, ns + n, cmp);
  if (!check(n, false)) {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 0; i < (n); i++) {
    if (ns[i].a && ns[i].b)
      n11.push_back(i);
    else if (ns[i].a)
      n10.push_back(i);
    else if (ns[i].b)
      n01.push_back(i);
    else
      n00.push_back(i);
  }
  int len = min(m, (int)n11.size());
  pos00 = 0;
  pos01 = 0;
  pos10 = 0;
  pos11 = len;
  sum = 0;
  now = len;
  now0 = now;
  now1 = now;
  mi = 10000000000000LL;
  ans00 = -1;
  ans01 = -1;
  ans10 = -1;
  ans11 = -1;
  for (int i = 0; i < (len); i++) sum += ns[n11[i]].t;
  bool flag = true;
  for (int i = len; i >= 0; i--) {
    while (now0 < k) {
      if (pos10 == n10.size()) {
        flag = false;
        break;
      }
      sum += ns[n10[pos10]].t;
      pos10++;
      now0++;
      now++;
    }
    if (!flag) break;
    while (now1 < k) {
      if (pos01 == n01.size()) {
        flag = false;
        break;
      }
      sum += ns[n01[pos01]].t;
      pos01++;
      now1++;
      now++;
    }
    if (!flag) break;
    while (now < m) {
      if (pos10 == n10.size() && pos01 == n01.size() && pos00 == n00.size()) {
        flag = false;
        break;
      }
      push();
    }
    if (!flag) break;
    if (now > m) {
      int rm = now - m;
      if (pos00 < rm) break;
      for (int j = 0; j < (rm); j++) {
        pos00--;
        now--;
        sum -= ns[n00[pos00]].t;
      }
    }
    if (sum < mi) {
      mi = sum;
      ans00 = pos00;
      ans01 = pos01;
      ans10 = pos10;
      ans11 = i;
    }
    now--;
    if (i) sum -= ns[n11[i - 1]].t;
    now0--;
    now1--;
  }
  cout << mi << '\n';
  for (int i = 0; i < (ans00); i++) cout << ns[n00[i]].ind << " ";
  for (int i = 0; i < (ans01); i++) cout << ns[n01[i]].ind << " ";
  for (int i = 0; i < (ans10); i++) cout << ns[n10[i]].ind << " ";
  for (int i = 0; i < (ans11); i++) cout << ns[n11[i]].ind << " ";
  return 0;
}
