#include <bits/stdc++.h>
using namespace std;
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
void foo() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<pair<int, int>> both;
  vector<pair<int, int>> alice;
  vector<pair<int, int>> bob;
  vector<pair<int, int>> none;
  for (int i = 0; i < n; i++) {
    int t, a, b;
    cin >> t >> a >> b;
    if (a && b) {
      both.push_back({t, i + 1});
    } else if (a) {
      alice.push_back({t, i + 1});
    } else if (b) {
      bob.push_back({t, i + 1});
    } else {
      none.push_back({t, i + 1});
    }
  }
  sort(both.begin(), both.end());
  sort(alice.begin(), alice.end());
  sort(bob.begin(), bob.end());
  sort(none.begin(), none.end());
  20;
  20;
  20;
  20;
  20;
  if ((int)both.size() + (int)min(alice.size(), bob.size()) < k) {
    cout << -1 << endl;
    return;
  }
  vector<int> ans;
  long long total = 0;
  int bi = 0;
  for (; bi < max(k * 2 - m, 0); bi++) {
    if (bi == (int)both.size()) {
      cout << -1 << endl;
      return;
    }
    total += both[bi].first;
    ans.push_back(both[bi].second);
  }
  20;
  int si = 0;
  priority_queue<pair<int, int>> pq;
  int in = bi;
  for (int i = bi; i < k; i++) {
    if (si < (int)min(alice.size(), bob.size()) && in + 2 <= m) {
      pq.push({alice[si].first, alice[si].second});
      pq.push({bob[si].first, bob[si].second});
      total += bob[si].first + alice[si].first;
      si++;
      in += 2;
    } else {
      ans.push_back(both[bi].second);
      total += both[bi].first;
      bi++;
      in++;
    }
  }
  20;
  vector<pair<int, int>> more;
  for (int i = si; i < (int)alice.size(); i++) {
    more.push_back(alice[i]);
  }
  for (int i = si; i < (int)bob.size(); i++) {
    more.push_back(bob[i]);
  }
  for (int i = 0; i < (int)none.size(); i++) {
    more.push_back(none[i]);
  }
  sort(more.begin(), more.end());
  for (int i = 0; in < m && i < (int)more.size(); in++, i++) {
    pq.push(more[i]);
    total += more[i].first;
  }
  20;
  for (; in < m; in++) {
    total += both[bi].first;
    ans.push_back(both[bi].second);
    bi++;
  }
  20;
  vector<pair<int, int>> out;
  long long best = total;
  long long pos = bi;
  for (; bi < (int)both.size() && pq.size();) {
    total += both[bi].first;
    ans.push_back(both[bi].second);
    bi++;
    pair<int, int> p = pq.top();
    out.push_back(p);
    pq.pop();
    total -= p.first;
    if (total < best) {
      best = total;
      pos = bi;
    }
    20;
  }
  20;
  20;
  for (int i = bi; i > pos; i--) {
    pair<int, int> add = out.back();
    out.pop_back();
    pq.push(add);
    ans.pop_back();
  }
  while (!pq.empty()) {
    pair<int, int> p = pq.top();
    ans.push_back(p.second);
    pq.pop();
  }
  cout << best << endl;
  for (int ind : ans) {
    cout << ind << " ";
  }
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  foo();
}
