#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
const double PI = acos(-1);
long long gcd() { return 0ll; }
template <typename T, typename... Args>
T gcd(T a, Args... args) {
  return __gcd(a, (__typeof(a))gcd(args...));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    long long n, m, k, t, a, b;
    cin >> n >> m >> k;
    vector<long long> va, vb, vab, nvab, vals;
    vector<long long> _va, _vb, _vab;
    long long x = 0, y = 0;
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      cin >> t >> a >> b;
      vals.emplace_back(t);
      if (a == 0 && b == 0) {
        nvab.emplace_back(i);
        continue;
      }
      x += a;
      y += b;
      if (a == 1 && b == 1)
        vab.emplace_back(i);
      else if (a == 1)
        va.emplace_back(i);
      else
        vb.emplace_back(i);
    }
    if (x < k || y < k) {
      cout << "-1\n";
      return 0;
    }
    auto fun = [&](long long x, long long y) -> bool {
      return vals[x] < vals[y];
    };
    sort((va).begin(), (va).end(), fun);
    sort((vb).begin(), (vb).end(), fun);
    sort((vab).begin(), (vab).end(), fun);
    _va = va;
    _vb = vb;
    _vab = vab;
    while (va.size() > k) va.pop_back();
    while (vb.size() > k) vb.pop_back();
    vector<long long> res;
    for (auto it : vab) {
      if (va.size() + res.size() != k || vb.size() + res.size() != k) {
        res.emplace_back(it);
        if (va.size() + res.size() > k) va.pop_back();
        if (vb.size() + res.size() > k) vb.pop_back();
        continue;
      }
      if (va.size() > 0 && vb.size() > 0) {
        if ((vals[va.back()] + vals[vb.back()]) > vals[it]) {
          va.pop_back();
          vb.pop_back();
          res.emplace_back(it);
        }
      } else {
        if (va.size() > 0) {
          if (vals[va.back()] > vals[it]) {
            va.pop_back();
            res.emplace_back(it);
          }
        } else if (vb.size() > 0) {
          if (vals[vb.back()] > vals[it]) {
            vb.pop_back();
            res.emplace_back(it);
          }
        }
      }
    }
    vector<bool> vis(n, 0);
    long long ans = 0;
    for (auto it : va) ans += vals[it], vis[it] = 1;
    for (auto it : vb) ans += vals[it], vis[it] = 1;
    for (auto it : res) ans += vals[it], vis[it] = 1;
    nvab.clear();
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      if (!vis[i]) nvab.emplace_back(i);
    sort((nvab).begin(), (nvab).end(), fun);
    if (m < (va.size() + vb.size() + res.size())) {
      va = _va;
      vb = _vb;
      vab = _vab;
      a = va.size();
      b = vb.size();
      t = vab.size();
      if ((a + t) < k || (b + t) < k) {
        cout << "-1\n";
        return 0;
      }
      vector<long long> pva(1, 0), pvb(1, 0);
      for (auto it : va) pva.emplace_back(pva.back() + vals[it]);
      for (auto it : vb) pvb.emplace_back(pvb.back() + vals[it]);
      long long sum = LLONG_MAX;
      long long temp;
      long long p = 0;
      bool f = 0;
      if (m == 2 * k && k <= va.size() && k <= vb.size()) {
        f = 1;
        sum = pva[k] + pvb[k];
        a = k;
        b = 0;
      }
      for (__typeof(vab.size()) i = (0) - ((0) > (vab.size()));
           i != (vab.size()) - ((0) > (vab.size()));
           i += 1 - 2 * ((0) > (vab.size()))) {
        p += vals[vab[i]];
        if ((2 * k - (i + 1)) != m || (k - (i + 1)) > va.size() ||
            (k - (i + 1)) > vb.size())
          continue;
        f = 1;
        temp = p + pva[k - (i + 1)] + pvb[k - (i + 1)];
        if (temp < sum) {
          sum = temp;
          a = k - (i + 1);
          b = i + 1;
        }
      }
      if (!f) {
        cout << "-1\n";
        return 0;
      }
      cout << sum << "\n";
      for (__typeof(a) i = (0) - ((0) > (a)); i != (a) - ((0) > (a));
           i += 1 - 2 * ((0) > (a)))
        cout << va[i] + 1 << " ";
      for (__typeof(a) i = (0) - ((0) > (a)); i != (a) - ((0) > (a));
           i += 1 - 2 * ((0) > (a)))
        cout << vb[i] + 1 << " ";
      for (__typeof(b) i = (0) - ((0) > (b)); i != (b) - ((0) > (b));
           i += 1 - 2 * ((0) > (b)))
        cout << vab[i] + 1 << " ";
      cout << "\n";
      return 0;
    }
    m -= va.size();
    m -= vb.size();
    m -= res.size();
    for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
         i += 1 - 2 * ((0) > (m)))
      ans += vals[nvab[i]];
    cout << ans << "\n";
    for (auto it : va) cout << it + 1 << " ";
    for (auto it : vb) cout << it + 1 << " ";
    for (auto it : res) cout << it + 1 << " ";
    for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
         i += 1 - 2 * ((0) > (m)))
      cout << nvab[i] + 1 << " ";
    cout << "\n";
  }
  return 0;
}
