#include <bits/stdc++.h>
using namespace std;
void buildsegment(unsigned long long int arr[], unsigned long long int tree[],
                  unsigned long long int n) {
  for (unsigned long long int i = 0; i < n; i++) tree[n + i] = arr[i];
  for (unsigned long long int i = n - 1; i > 0; --i)
    tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
void updatesegment(unsigned long long int p, unsigned long long int value,
                   unsigned long long int tree[], unsigned long long int n) {
  tree[p + n] = value;
  p = p + n;
  for (unsigned long long int i = p; i > 1; i >>= 1)
    tree[i >> 1] = tree[i] + tree[i ^ 1];
}
unsigned long long int querysegment(unsigned long long int l,
                                    unsigned long long int r,
                                    unsigned long long int tree[],
                                    unsigned long long int n) {
  unsigned long long int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res += tree[l++];
    if (r & 1) res += tree[--r];
  }
  return res;
}
long long int findpar(long long int numb, long long int par[]) {
  if (par[numb] == numb)
    return numb;
  else
    return par[numb] = findpar(par[numb], par);
}
void setpar(long long int u1, long long int v1, long long int par[]) {
  long long int s1 = findpar(v1, par);
  long long int s2 = findpar(u1, par);
  if (s1 < s2) {
    par[s2] = s1;
  } else {
    par[s1] = s2;
  }
}
unsigned long long int extended_GCD(unsigned long long int a,
                                    unsigned long long int b,
                                    unsigned long long int &x,
                                    unsigned long long int &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  unsigned long long int x1, y1;
  unsigned long long int gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
unsigned long long int modinv(unsigned long long int a,
                              unsigned long long int mod = 1000000007) {
  unsigned long long int x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
unsigned long long int power(unsigned long long int a, unsigned long long int b,
                             unsigned long long int m = 1000000007) {
  a %= m;
  unsigned long long int ans = 1;
  while (b > 0) {
    if (b & 1) ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return ans;
}
vector<unsigned long long int> nge(unsigned long long int arr[],
                                   unsigned long long int n) {
  stack<unsigned long long int> s;
  vector<unsigned long long int> ans(n, -1);
  s.push(0);
  for (unsigned long long int i = 1; i < n; i++) {
    if (s.empty()) {
      s.push(i);
      continue;
    }
    while (s.empty() == false && arr[s.top()] < arr[i]) {
      ans[s.top()] = i;
      s.pop();
    }
    s.push(arr[i]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  unsigned long long int n, k, m;
  cin >> n >> m >> k;
  vector<pair<unsigned long long int, unsigned long long int>> a, b, d;
  unsigned long long int n1, n2, n3, chos = 0;
  pair<unsigned long long int, unsigned long long int> arr[n];
  for (unsigned long long int i = 0; i < n; i++) {
    cin >> n1 >> n2 >> n3;
    arr[i] = {n1, i};
    if (n3 == n2 && n2 == 1) {
      d.push_back({n1, i});
    } else if (n2 == 1) {
      a.push_back({n1, i});
    } else if (n3 == 1) {
      b.push_back({n1, i});
    }
  }
  vector<pair<unsigned long long int, unsigned long long int>> fa, fb;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(d.begin(), d.end());
  unsigned long long int x = min(a.size(), b.size());
  map<unsigned long long int, unsigned long long int> mp;
  if (x > k) x = k;
  unsigned long long int ans = 0, dc = 0;
  if (x + d.size() >= k) {
    if (x < k) {
      for (unsigned long long int i = 0; i < x; i++) {
        ans += a[i].first + b[i].first;
        fa.push_back(a[i]);
        fb.push_back(b[i]);
        chos += 2;
        mp[a[i].second] = 1;
        mp[b[i].second] = 1;
      }
      for (unsigned long long int i = 0; i <= k - x - 1; i++) {
        ans += d[i].first;
        chos += 1;
        mp[d[i].second] = 1;
      }
      dc = k - x;
    } else {
      for (unsigned long long int i = 0; i < k; i++) {
        ans += a[i].first + b[i].first;
        fa.push_back(a[i]);
        fb.push_back(b[i]);
        chos += 2;
        mp[a[i].second] = 1;
        mp[b[i].second] = 1;
      }
    }
    unsigned long long int non = fa.size() - 1;
    if (x > 0) {
      while (dc < d.size()) {
        if (fa[non].first + fb[non].first > d[dc].first || chos > m) {
          ans -= fa[non].first + fb[non].first - d[dc].first;
          mp[fa[non].second] = 0;
          mp[fb[non].second] = 0;
          mp[d[dc].second] = 1;
          chos--;
        } else
          break;
        dc++;
        non--;
        if (non < 0) break;
      }
    }
    if (chos <= m) {
      sort(arr, arr + n);
      unsigned long long int count = 0;
      if (chos < m) {
        for (unsigned long long int i = 0; i < n; i++) {
          if (mp[arr[i].second] == 0) {
            ans += arr[i].first;
            count++;
            mp[arr[i].second] = 1;
            if (count + chos == m) break;
          }
        }
      }
      cout << ans << endl;
      for (auto vn : mp) {
        if (vn.second == 1) cout << vn.first + 1 << " ";
      }
      cout << endl;
    } else
      cout << -1 << endl;
  } else
    cout << -1 << endl;
  return 0;
}
