#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define lsc(x)      scanf("%lld",&x)
#define sc(x)       scanf("%d",&x)
#define lpr(x)      printf("%lld ",x)
#define pr(x)       printf("%d ",x)
#define n_l         printf("\n")
#define VI          vector<int>
#define VII         vector<long long int>
#define m_p         make_pair
#define pb          push_back
#define fi           first
#define se           second
#define mset(x,y)   memset(x,y,sizeof(x))
#define sz(v)       (int)v.size()
#define all(v)      v.begin(), v.end()
#define fr(i, a, n) for(int i=a;i<=n;i++)
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
const int N=(int)1e6+5;
const int mod = 1000000007;
typedef long long ll;
// order_of_key (val): returns the no. of values strictly less than val
// find_by_order (k): returns the kth largest element iterator.(0-based)
// vector<int>::iterator itr=lower_bound(v.begin(),v.end(),x);
// s.substr(pos[0-indexed], len(default=till end))
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

ll fmod(ll x){if (x<mod) return x;return x%mod;}
ll mul(ll a, ll b, ll c){
	ll ret=0;while(b){if (b%2) ret=(ret+a)%c;a=(a*2)%c;b>>=1;}
    return ret;
}
int modpow(ll a, ll b){
    ll ret=1;while(b){if (b%2) ret=(ret*a)%mod;a=(a*a)%mod;b>>=1;}
    return (int)ret;
}
inline int inv(int x){ return modpow(x, mod-2);}
int isprime[N];
void calc_prime(){
	isprime[1]=1;for(ll i=2;i<N;i++) if (!isprime[i]) for(ll j=i*i;j<N;j+=i) isprime[j]=1;
}
set<pair<int, int> > sa, sb, s, rem, ss;
VI va, vb;
int lk[N], ti[N];
set<int> fn;
int main(){
    int n, m, k;sc(n);sc(m);sc(k);
    int cnta=0;
    int cntb=0;
    fr(i, 1, n){
        int a, b;
        sc(ti[i]);sc(a);sc(b);
        lk[i] = a*2 + b;
        if (a) {
            cnta++;
            sa.insert({ti[i], i});
        }
        if (b) {
            cntb++;
            sb.insert({ti[i], i});
        }
        if (a and b){
            s.insert({ti[i], i});
        }
        rem.insert({ti[i], i});
    }
    s.insert({mod, -1});
    sa.insert({mod, -1});
    sb.insert({mod, -1});
    if (cnta<k or cntb<k){
        pr(-1);n_l;return 0;
    }
    int ans=0;
    cnta = k;
    cntb = k;
    //trace(cnta, cntb, s.size(), sa.size(), sb.size());
    int bk = 0;
    while(cnta > 0 and cntb > 0){
        auto ppa = *sa.begin();
        auto ppb = *sb.begin();
        auto pp = *s.begin();
        if (ppa.fi + ppb.fi < pp.fi){
            ans += ppa.fi + ppb.fi;
            if (lk[ppa.se]) s.erase(ppa);
            if (lk[ppb.se]) s.erase(ppb);
            sa.erase(ppa);
            sb.erase(ppb);
            rem.erase(ppa);rem.erase(ppb);
            va.pb(ppa.se);
            vb.pb(ppb.se);
            fn.insert(ppa.se);
            fn.insert(ppb.se);
            bk+=2;
        }
        else{
            ans+=pp.fi;
            if (lk[pp.se]&2) sa.erase(pp);
            if (lk[pp.se]%2) sb.erase(pp);
            fn.insert(pp.se);
            rem.erase(pp);
            s.erase(pp);
            pp.fi*=-1;
            ss.insert(pp);
            bk+=1;
        }
        cnta--;
        cntb--;
        //trace(ans, bk, cnta, cntb, s.size(), sa.size(), sb.size());
    }
    //trace(ans, bk, m);
    /*for(auto it: fn) pr(it);n_l;
    cout<<"va: ";
    for(auto it: va) pr(it);n_l;
    cout<<"vb: ";
    for(auto it: vb) pr(it);n_l;
    */
    if (bk>m){
        if(s.size() <= bk-m){
            pr(-1);n_l;
            return 0;
        }
        else{
            fr(i, 1, bk-m){
                auto pp = *s.begin();
                s.erase(pp);
                fn.insert(pp.se);
                ans += pp.fi;
                fn.erase(va.back());
                fn.erase(vb.back());
                ans -= ti[va.back()]; va.pop_back();
                ans -= ti[vb.back()]; vb.pop_back();
            }
        }
    }
    while (bk<m){
        bk++;
        auto ppr = *rem.begin();
        auto pp = *ss.begin();
        auto ppa = *sa.begin();
        auto ppb = *sb.begin();
        //trace(ppa.fi+ ppb.fi, ppr.fi- pp.fi);
        if (-pp.fi + ppr.fi <= ppa.fi + ppb.fi){
            rem.erase(ppr);ans+=ppr.fi;
            fn.insert(ppr.se);
            if (sa.find(ppr)!=sa.end()) sa.erase(ppr);
            if (sb.find(ppr)!=sb.end()) sb.erase(ppr);
        }
        else{
            fn.erase(pp.se);
            fn.insert(ppa.fi);
            fn.insert(ppb.fi);
            sa.erase(ppa);
            sb.erase(ppb);
            rem.erase(ppa), rem.erase(ppb);
            ss.erase(pp);
            if (pp.fi!=0) rem.insert({-pp.fi, pp.se});
            if (!sz(ss)) ss.insert({0, -1});
            ans += pp.fi + ppa.fi + ppb.fi;
        }
    }
    pr(ans);n_l;
    for(auto it: fn) pr(it);n_l;
    return 0;
}

