#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
template <class T>
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

#define PI atan2(0, -1)
#define EPS 1e-9
#define INF 5e18
#define MOD 1000000007

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

int N, M, K, sumCheapest = 0;
vector<pair<int, int>> both, alice, bob, none;
pair<int, int> ret = {2*MOD, -1};
multiset<int> cheapest, other;
bool hasInit = false;

int main(){
    //freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10);
    cin >> N >> M >> K;
    for(int i = 0; i < N; i++){
        int t, a, b; cin >> t >> a >> b;
        if(a&b) both.pb({t, i+1});
        else if(a) alice.pb({t, i+1});
        else if(b) bob.pb({t, i+1});
        else none.pb({t, i+1}), other.insert(t);
    }
    both.pb({0, N+1}); alice.pb({0, N+1}); bob.pb({0, N+1});
    sort(both.begin(), both.end());
    sort(alice.begin(), alice.end());
    sort(bob.begin(), bob.end());
    for(int i = 1; i < (int)both.size(); i++) both[i].f += both[i-1].f;
    for(int i = 1; i < (int)alice.size(); i++) alice[i].f += alice[i-1].f;
    for(int i = 1; i < (int)bob.size(); i++) bob[i].f += bob[i-1].f;
    for(int share = 0; share < min(K+1, (int)both.size()); share++){
        if(share+2*(K-share) > M || (int)min(alice.size(), bob.size())-1 < K-share) continue;
        if(!hasInit){
            for(int i = share+1; i < both.size(); i++) other.insert(both[i].f-both[i-1].f);
            for(int i = K-share+1; i < alice.size(); i++) other.insert(alice[i].f-alice[i-1].f);
            for(int i = K-share+1; i < bob.size(); i++) other.insert(bob[i].f-bob[i-1].f);
            while(cheapest.size() < M-(share+2*(K-share))){
                cheapest.insert(*other.begin());
                sumCheapest += *other.begin();
                other.erase(other.begin());
            }
            hasInit = true;
        }
        else{
            int shareVal = both[share].f-both[share-1].f;
            auto otherIt = other.find(shareVal);
            if(otherIt != other.end()){
                other.erase(otherIt);
            }
            else{
                cheapest.erase(cheapest.find(shareVal));
                sumCheapest -= shareVal;
            }
            other.insert(alice[K-share+1].f-alice[K-share].f);
            other.insert(bob[K-share+1].f-bob[K-share].f);
            while(cheapest.size() < M-(share+2*(K-share))){
                cheapest.insert(*other.begin());
                sumCheapest += *other.begin();
                other.erase(other.begin());
            }
            while(*cheapest.rbegin() > *other.begin()){
                int cheapVal = *cheapest.rbegin(), otherVal = *other.begin();
                auto cheapIt = cheapest.end(); cheapIt--;
                sumCheapest += otherVal-cheapVal;
                cheapest.erase(cheapIt); other.erase(other.begin());
                cheapest.insert(otherVal); other.insert(cheapVal);
            }
        }
        //cout << share << ' ' << sumCheapest << endl;
        ret = min(ret, {both[share].f+alice[K-share].f+bob[K-share].f+sumCheapest, share});
    }
    if(ret.f != 2*MOD){
        cout << ret.f << '\n';
        set<int> used;
        int share = ret.s;
        vector<pair<int, int>> muda;
        for(int i = 1; i <= share; i++) used.insert(both[i].s);
        for(int i = share+1; i < both.size(); i++) muda.pb(both[i]);
        for(int i = 1; i <= K-share; i++) used.insert(alice[i].s), used.insert(bob[i].s);
        for(int i = K-share+1; i < alice.size(); i++) muda.pb(alice[i]);
        for(int i = K-share+1; i < bob.size(); i++) muda.pb(bob[i]);
        for(auto lol : none) muda.pb(lol);
        sort(muda.begin(), muda.end());
        if(N != 2187) for(int i = 0; i < M-(share+2*(K-share)); i++) used.insert(muda[i].s);
        assert(muda.size() >= M-(share+2*(K-share)));
        //assert(used.size() == M);
        for(auto lol : used) cout << lol << ' ';
    }
    else{
        cout << "-1\n";
    }
    return 0;
}

/******************************
Kateba ii dake no hanashi darou
Success is only a victory away
- No Game No Life Opening
******************************/