#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
inline int add(int a, int b) {
  a += b;
  return a >= 1000000007 ? a - 1000000007 : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + 1000000007 : a;
}
inline int mul(int a, int b) { return (long long int)a * b % 1000000007; }
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Cheek(int N, int pos) { return (bool)(N & (1 << pos)); }
long long int a[200005], b[200005], tim[200005];
void Solve(int t) {
  priority_queue<pair<long long int, long long int> > both, alice, bob, ajaira;
  long long int i, j, k, n, m, boblike = 0, alicelike = 0, ans = 0, atake = 0,
                               btake = 0;
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++) {
    cin >> tim[i] >> a[i] >> b[i];
    if (a[i] && b[i]) {
      alicelike++;
      boblike++;
      both.push({-tim[i], i});
    } else if (a[i]) {
      alicelike++;
      alice.push({-tim[i], i});
    } else if (b[i]) {
      boblike++;
      bob.push({-tim[i], i});
    } else
      ajaira.push({-tim[i], i});
  }
  if (alicelike < k || boblike < k) {
    printf("-1\n");
    return;
  }
  priority_queue<pair<long long int, long long int> > taken1, taken2, taken3;
  while (atake < k && !alice.empty()) {
    ans += ((-1) * alice.top().first);
    taken1.push({-alice.top().first, alice.top().second});
    alice.pop();
    atake++;
  }
  while (btake < k && !bob.empty()) {
    ans += ((-1) * bob.top().first);
    taken2.push({-bob.top().first, bob.top().second});
    bob.pop();
    btake++;
  }
  long long int sz1 = taken1.size();
  long long int sz2 = taken2.size();
  while (sz1 != sz2) {
    if (sz1 > sz2) {
      ans -= taken1.top().first;
      alice.push({-taken1.top().first, taken1.top().second});
      taken1.pop();
      sz1--;
      atake--;
    } else {
      ans -= taken2.top().first;
      bob.push({-taken2.top().first, taken2.top().second});
      taken2.pop();
      sz2--;
      btake--;
    }
  }
  while (!both.empty() && (atake < k || btake < k)) {
    ans += (-both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
    atake++;
    btake++;
  }
  while (
      !both.empty() && !taken1.empty() && !taken2.empty() &&
      ((-1) * both.top().first < (taken1.top().first + taken2.top().first))) {
    ans -= taken1.top().first;
    ans -= taken2.top().first;
    alice.push({-taken1.top().first, taken1.top().second});
    bob.push({-taken2.top().first, taken2.top().second});
    taken1.pop();
    taken2.pop();
    ans += ((-1) * both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
  }
  sz1 = taken1.size();
  sz2 = taken2.size();
  long long int sz3 = taken3.size();
  while (sz1 + sz2 + sz3 < m && !taken3.empty() && !alice.empty() &&
         !bob.empty()) {
    long long int now = taken3.top().first;
    long long int hobe = -(alice.top().first + bob.top().first);
    long long int xtra = hobe - now;
    bool fg = 1;
    if (!alice.empty() && -alice.top().first < xtra) fg = 0;
    if (!bob.empty() && -bob.top().first < xtra) fg = 0;
    if (!ajaira.empty() && -ajaira.top().first < xtra) fg = 0;
    if (fg) {
      ans -= taken3.top().first;
      both.push(make_pair(-taken3.top().first, taken3.top().second));
      taken3.pop();
      sz3--;
      ans += -alice.top().first;
      taken1.push(make_pair(-alice.top().first, alice.top().second));
      alice.pop();
      sz1++;
      ans += -bob.top().first;
      taken2.push(make_pair(-bob.top().first, bob.top().second));
      bob.pop();
      sz2++;
    } else {
      int take = 0;
      pair<long long int, long long int> pr1 = {(1 << 28), (1 << 28)};
      if (!alice.empty() && -alice.top().first < pr1.first) {
        pr1 = {-alice.top().first, alice.top().second};
        take = 1;
      }
      if (!bob.empty() && -bob.top().first < pr1.first) {
        pr1 = {-bob.top().first, bob.top().second};
        take = 2;
      }
      if (!both.empty() && -both.top().first < pr1.first) {
        pr1 = {-both.top().first, both.top().second};
        take = 3;
      }
      if (!ajaira.empty() && -ajaira.top().first < pr1.first) {
        pr1 = {-ajaira.top().first, ajaira.top().second};
        take = 4;
      }
      if (take == 1)
        alice.pop();
      else if (take == 2)
        bob.pop();
      else if (take == 3)
        both.pop();
      else
        ajaira.pop();
      ans += pr1.first;
      taken3.push(pr1);
      sz3++;
    }
  }
  while (sz1 + sz2 + sz3 < m &&
         (!alice.empty() || !bob.empty() || !both.empty())) {
    int fg = 0;
    pair<long long int, long long int> pr1 = {(1 << 28), (1 << 28)};
    if (!alice.empty() && -alice.top().first < pr1.first) {
      pr1 = {-alice.top().first, alice.top().second};
      fg = 1;
    }
    if (!bob.empty() && -bob.top().first < pr1.first) {
      pr1 = {-bob.top().first, bob.top().second};
      fg = 2;
    }
    if (!both.empty() && -both.top().first < pr1.first) {
      pr1 = {-both.top().first, both.top().second};
      fg = 3;
    }
    if (!ajaira.empty() && -ajaira.top().first < pr1.first) {
      pr1 = {-ajaira.top().first, ajaira.top().second};
      fg = 4;
    }
    if (fg == 1)
      alice.pop();
    else if (fg == 2)
      bob.pop();
    else if (fg == 3)
      both.pop();
    else
      ajaira.pop();
    ans += pr1.first;
    taken3.push(pr1);
    sz3++;
  }
  while (sz1 + sz2 + sz3 > m && !both.empty() && sz1 > 0 && sz2 > 0) {
    ans -= taken1.top().first;
    taken1.pop();
    ans -= taken2.top().first;
    taken2.pop();
    ans += (-both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
    sz1--;
    sz2--;
    sz3++;
  }
  if (sz1 + sz2 + sz3 != m) {
    cout << "-1\n";
    return;
  }
  cout << ans << endl;
  while (!taken1.empty()) {
    cout << taken1.top().second << ' ';
    taken1.pop();
  }
  while (!taken2.empty()) {
    cout << taken2.top().second << ' ';
    taken2.pop();
  }
  while (!taken3.empty()) {
    cout << taken3.top().second << ' ';
    taken3.pop();
  }
  cout << endl;
}
int main() {
  int t, T;
  T = 1;
  for (__typeof(t) t = 1; t <= T; t++) {
    Solve(t);
  }
  return 0;
}
