#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000LL;
vector<pair<long long, int> > mejores(vector<pair<long long, int> >& fi,
                                      vector<pair<long long, int> >& se,
                                      vector<pair<long long, int> >& te,
                                      int cantidad, int pf, int ps, int pt) {
  vector<pair<long long, int> > result;
  while (result.size() < cantidad) {
    long long vf = INF;
    if (pf < fi.size()) vf = fi[pf].first;
    long long vs = INF;
    if (ps < se.size()) vs = se[ps].first;
    long long vt = INF;
    if (pt < te.size()) vt = te[pt].first;
    if (vf <= vs && vf <= vt) {
      result.push_back(fi[pf]);
      pf++;
    } else if (vs <= vf && vs <= vt) {
      result.push_back(se[ps]);
      ps++;
    } else {
      result.push_back(te[pt]);
      pt++;
    }
  }
  return result;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<pair<long long, int> > uno;
  vector<pair<long long, int> > dos;
  vector<pair<long long, int> > juntos;
  vector<pair<long long, int> > solos;
  set<long long> q;
  int b, c;
  long long a;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    if (b == 1 && c == 1) {
      juntos.push_back(make_pair(a, i + 1));
    } else if (b == 1 && c == 0) {
      uno.push_back(make_pair(a, i + 1));
    } else if (b == 0 && c == 1) {
      dos.push_back(make_pair(a, i + 1));
    } else {
      solos.push_back(make_pair(a, i + 1));
    }
  }
  sort(uno.begin(), uno.end());
  sort(dos.begin(), dos.end());
  sort(juntos.begin(), juntos.end());
  sort(solos.begin(), solos.end());
  if (juntos.size() + uno.size() < k || juntos.size() + dos.size() < k) {
    cout << -1 << '\n';
  } else {
    vector<long long> usum;
    vector<long long> dsum;
    vector<long long> jsum;
    jsum.push_back(0);
    usum.push_back(0);
    dsum.push_back(0);
    for (int i = 0; i < uno.size(); i++) {
      usum.push_back(usum[i] + uno[i].first);
    }
    for (int i = 0; i < dos.size(); i++) {
      dsum.push_back(dsum[i] + dos[i].first);
    }
    for (int i = 0; i < juntos.size(); i++) {
      jsum.push_back(jsum[i] + juntos[i].first);
    }
    long long result = INF;
    int index;
    long long suma = 0;
    for (int i = 0; i <= juntos.size(); i++) {
      int j = i;
      int u = max(k - j, 0);
      int d = max(k - j, 0);
      int falta = m - j - u - d;
      if (q.size() != 0) {
        suma += uno[u].first;
        suma += dos[d].first;
        q.insert(uno[u].first);
        q.insert(dos[d].first);
        auto it = q.rbegin();
        suma -= *(it);
        q.erase(*(it));
      }
      if (u > uno.size() || d > dos.size() || j + u + d > m ||
          falta > solos.size() + uno.size() - u + dos.size() - d)
        continue;
      if (q.size() == 0) {
        int pu = u;
        int pd = d;
        int pv = 0;
        vector<pair<long long, int> > bestis =
            mejores(uno, dos, solos, falta, u, d, 0);
        for (pair<long long, int> it : bestis) {
          suma += it.first;
          q.insert(it.first);
        }
      }
      long long current = jsum[j] + usum[u] + dsum[d] + suma;
      if (current < result) {
        result = current;
        index = i;
      }
    }
    if (result >= INF) {
      cout << -1 << '\n';
    } else {
      cout << result << '\n';
      vector<long long> ans;
      for (int i = 0; i < index; i++) {
        ans.push_back(juntos[i].second);
      }
      int fill = max(k - index, 0);
      for (int i = 0; i < fill; i++) {
        ans.push_back(uno[i].second);
      }
      for (int i = 0; i < fill; i++) {
        ans.push_back(dos[i].second);
      }
      int falta = m - index - fill - fill;
      vector<pair<long long, int> > bestis =
          mejores(uno, dos, solos, falta, fill, fill, 0);
      for (pair<long long, int> it : bestis) {
        ans.push_back(it.second);
      }
      for (long long it : ans) {
        cout << it << " ";
      }
      cout << '\n';
    }
  }
  return 0;
}
