#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<pair<int, int> > A, B, C;
  vector<pair<int, int> > Ataken, Btaken;
  vector<pair<int, int> > ABC;
  set<int> taken;
  int ac = 0;
  int bc = 0;
  int t = 0;
  int read = 0;
  for (int i = 0; i < n; i++) {
    int tu, a, b;
    cin >> tu >> a >> b;
    if (a && !b) A.push_back({tu, i});
    if (b && !a) B.push_back({tu, i});
    if (a && b) C.push_back({tu, i});
    if (!a && !b) ABC.push_back({tu, i});
  }
  sort(A.begin(), A.end(), greater<pair<int, int> >());
  sort(B.begin(), B.end(), greater<pair<int, int> >());
  sort(C.begin(), C.end(), greater<pair<int, int> >());
  while (ac < k || bc < k) {
    int use = -1;
    if (ac < k && bc < k) {
      if (A.empty()) {
        if (C.empty()) {
          use = -2;
        } else {
          use = 2;
        }
      } else if (B.empty()) {
        if (C.empty()) {
          use = -2;
        } else {
          use = 2;
        }
      } else {
        assert(!A.empty());
        assert(!B.empty());
        if (C.empty()) {
          use = 3;
        } else {
          if (A.back().first + B.back().first < C.back().first) {
            use = 3;
          } else {
            use = 2;
          }
        }
      }
    } else if (ac < k) {
      if (A.empty()) {
        if (C.empty()) {
          use = -2;
        } else {
          use = 2;
        }
      } else {
        if (C.empty()) {
          use = 0;
        } else {
          if (A.back().first < C.back().first) {
            use = 0;
          } else {
            use = 2;
          }
        }
      }
    } else if (bc < k) {
      if (B.empty()) {
        if (C.empty()) {
          use = -2;
        } else {
          use = 2;
        }
      } else {
        if (C.empty()) {
          use = 2;
        } else {
          if (B.back().first < C.back().first) {
            use = 1;
          } else {
            use = 2;
          }
        }
      }
    } else {
    }
    if (use == 0 || use == 3) {
      assert(!A.empty());
      t += A.back().first;
      taken.insert(A.back().second);
      Ataken.push_back(A.back());
      A.pop_back();
      ac++;
      read++;
    }
    if (use == 1 || use == 3) {
      assert(!B.empty());
      taken.insert(B.back().second);
      Btaken.push_back(B.back());
      t += B.back().first;
      B.pop_back();
      bc++;
      read++;
    }
    if (use == 2) {
      assert(!C.empty());
      taken.insert(C.back().second);
      t += C.back().first;
      C.pop_back();
      ac++;
      bc++;
      read++;
    }
    if (use < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  while (read > m) {
    if (C.empty()) {
      cout << -1 << endl;
      return 0;
    }
    if (!Ataken.empty() && !Btaken.empty()) {
      t -= Ataken.back().first;
      t -= Btaken.back().first;
      taken.erase(Ataken.back().second);
      taken.erase(Btaken.back().second);
      Ataken.pop_back();
      Btaken.pop_back();
      t += C.back().first;
      taken.insert(C.back().second);
      C.pop_back();
      read--;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  for (auto& tt : A) ABC.push_back(tt);
  for (auto& tt : B) ABC.push_back(tt);
  for (auto& tt : C) ABC.push_back(tt);
  sort(ABC.begin(), ABC.end(), greater<pair<int, int> >());
  while (read < m) {
    t += ABC.back().first;
    taken.insert(ABC.back().second);
    ABC.pop_back();
    read++;
  }
  cout << t << endl;
  assert((int)taken.size() == m);
  for (int u : taken) {
    cout << u + 1 << " ";
  }
  cout << endl;
}
