#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, k, m;
  cin >> n >> m >> k;
  vector<pair<long long int, long long int> > common;
  vector<pair<long long int, long long int> > single_a;
  vector<pair<long long int, long long int> > single_b;
  vector<pair<long long int, long long int> > spare;
  for (long long int j = 0; j < n; j++) {
    long long int t, a, b;
    cin >> t >> a >> b;
    if (a == 0 && b == 0)
      spare.push_back({t, j + 1});
    else if (a == 1 && b == 1)
      common.push_back({t, j + 1});
    else if (a == 1 && b == 0)
      single_a.push_back({t, j + 1});
    else
      single_b.push_back({t, j + 1});
  }
  sort(common.begin(), common.end());
  sort(single_a.begin(), single_a.end());
  sort(single_b.begin(), single_b.end());
  sort(spare.begin(), spare.end());
  long long int combined =
      common.size() + min(single_a.size(), single_b.size());
  if (combined < k)
    cout << -1 << "\n";
  else {
    vector<long long int> ansv;
    vector<pair<long long int, pair<long long int, long long int> > >
        single_use;
    for (long long int j = 0; j < min(single_a.size(), single_b.size()); j++)
      single_use.push_back({single_a[j].first + single_b[j].first,
                            {single_a[j].second, single_b[j].second}});
    long long int p1 = 0;
    long long int p2 = 0;
    long long int ans = 0;
    long long int num = 0;
    while (k > 0) {
      if (p1 < common.size() && p2 < single_use.size()) {
        if (common[p1].first < single_use[p2].first) {
          ans = ans + common[p1].first;
          ansv.push_back(common[p1].second);
          p1++;
          num++;
        } else {
          ans = ans + single_use[p2].first;
          ansv.push_back(single_use[p2].second.first);
          ansv.push_back(single_use[p2].second.second);
          p2++;
          num += 2;
        }
      } else if (p1 == common.size()) {
        ans = ans + single_use[p2].first;
        ansv.push_back(single_use[p2].second.first);
        ansv.push_back(single_use[p2].second.second);
        p2++;
        num += 2;
      } else if (p2 == single_use.size()) {
        ans = ans + common[p1].first;
        ansv.push_back(common[p1].second);
        p1++;
        num++;
      }
      k--;
    }
    if (num <= m) {
      if (n == 19683) {
        cout << "YO " << num << " " << m << " " << p1 << " " << p2 << " "
             << endl;
        cout << common.size() << " " << spare.size() << endl;
      }
      long long int p2_a = p2;
      long long int p2_b = p2;
      long long int p_s = 0;
      vector<pair<long long int, long long int> > naya;
      for (long long int j = p1; j < common.size(); j++)
        naya.push_back(common[j]);
      for (long long int j = p2_a; j < single_a.size(); j++)
        naya.push_back(single_a[j]);
      for (long long int j = p2_b; j < single_b.size(); j++)
        naya.push_back(single_b[j]);
      for (long long int j = p_s; j < spare.size(); j++)
        naya.push_back(spare[j]);
      sort(naya.begin(), naya.end());
      long long int index = 0;
      cout << ans << "\n";
      for (long long int j = 0; j < ansv.size(); j++) cout << ansv[j] << " ";
      cout << "\n";
    } else {
      if (n == 19683)
        cout << "HI"
             << "\n";
      long long int diff = num - m;
      long long int available_common = common.size() - p1;
      if (diff > available_common)
        cout << -1 << "\n";
      else {
        while (diff > 0) {
          p2--;
          ans = ans - single_use[p2].first;
          ans = ans + common[p1].first;
          p1++;
          diff--;
        }
        cout << ans << "\n";
        for (long long int j = 0; j < p1; j++) cout << common[j].second << " ";
        for (long long int j = 0; j < p2; j++)
          cout << single_use[j].second.first << " "
               << single_use[j].second.second;
        cout << "\n";
      }
    }
  }
}
