#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
struct entry {
  ll t, a, b, type, i;
  bool operator<(const entry& o) const { return t < o.t; }
};
constexpr ll inf = 0x3FFFFFFFFFFF;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<deque<pair<ll, ll>>> in(4);
  vector<entry> in2(n);
  for (ll i = 0; i < n; i++) {
    ll t, ai, bi;
    cin >> t >> ai >> bi;
    ll type;
    if (ai != 0 && bi != 0) {
      type = 0;
      in[0].push_back({t, i});
    } else if (ai != 0) {
      type = 1;
      in[1].push_back({t, i});
    } else if (bi != 0) {
      type = 2;
      in[2].push_back({t, i});
    } else {
      type = 3;
      in[3].push_back({t, i});
    }
    in2[i] = {t, ai, bi, type, i};
  }
  for (auto& v : in) sort(begin(v), end(v));
  vector<priority_queue<pair<ll, ll>>> res(4);
  while (k * 2 > m && !in[0].empty()) {
    res[0].push(in[0][0]);
    in[0].pop_front();
    k--;
    m--;
  }
  if (k * 2 > m) {
    cout << -1 << endl;
    return 0;
  }
  while ((!in[0].empty() || (!in[1].empty() && !in[2].empty())) && k > 0) {
    if (in[1].empty() || in[2].empty() ||
        (!in[0].empty() && in[0][0].first < in[1][0].first + in[2][0].first)) {
      cout << boolalpha << in[0].empty() << endl;
      res[0].push(in[0][0]);
      in[0].pop_front();
      k--;
      m--;
    } else {
      cout << boolalpha << in[1].empty() << " " << in[2].empty() << endl;
      res[1].push(in[1][0]);
      in[1].pop_front();
      res[2].push(in[2][0]);
      in[2].pop_front();
      k--;
      m -= 2;
    }
  }
  if (k > 0) {
    cout << -1 << endl;
    return 0;
  }
  deque<entry> missing;
  for (auto& v : in) {
    for (auto e : v) {
      missing.push_back(in2[e.second]);
    }
  }
  sort(begin(missing), end(missing));
  ll extraA = 0;
  ll extraB = 0;
  while (m > 0) {
    auto e = missing.front();
    missing.pop_front();
    if (e.a != 0) extraA++;
    if (e.b != 0) extraB++;
    res[e.type].push({e.t, e.i});
    m--;
  }
  for (auto e : missing) {
    if (e.type == 0) {
      extraA++;
      extraB++;
      res[0].push({e.t, e.i});
    } else if (e.type == 1) {
      extraA++;
      res[1].push({e.t, e.i});
    } else if (e.type == 2) {
      extraB++;
      res[2].push({e.t, e.i});
    } else {
      res[3].push({e.t, e.i});
    }
    ll rem = -1;
    ll change = -1;
    if (!in[3].empty() && res[3].top().first > change) {
      rem = 3;
      change = res[3].top().first;
    }
    if (extraB > 0 && !in[2].empty() && res[2].top().first > change) {
      rem = 2;
      change = res[2].top().first;
    }
    if (extraA > 0 && !in[1].empty() && res[1].top().first > change) {
      rem = 1;
      change = res[1].top().first;
    }
    if (extraA > 0 && extraB > 0 && !in[0].empty() &&
        res[0].top().first > change) {
      rem = 0;
      change = res[0].top().first;
    }
    res[rem].pop();
    if (rem == 0) {
      extraA--;
      extraB--;
    } else if (rem == 1) {
      extraA--;
    } else if (rem == 2) {
      extraB--;
    } else {
    }
  }
  ll time = 0;
  vector<ll> ans;
  for (auto& v : res) {
    while (!v.empty()) {
      auto e = v.top();
      v.pop();
      ans.push_back(e.second);
      time += e.first;
    }
  }
  cout << time << endl;
  for (ll x : ans) {
    cout << x + 1 << " ";
  }
  cout << endl;
}
