/// Code by Leonardo16
///   “It is not the strength of the body, but the strength of the spirit.”   —  J.R.R. Tolkien
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,m,k;
    cin>>n>>m>>k;

    vector<int>tog;
    vii t2;
    map<ii,int>alice,bob,rem,mr,ma,mb;


    for(int i=0;i<n;i++){
        int t,a,b;
        cin>>t>>a>>b;

        if(a==1 && b==1 ){
            tog.pb(t);
            t2.pb({t,i+1});
        }else{
            if(a==1){
                alice[{t,i+1}]++;
                ma[{t,i+1}]++;
            }
            if(b==1){
                bob[{t,i+1}]++;
                mb[{t,i+1}]++;
            }

            if(a==0 && b==0){
                rem[{t,i+1}]++;
                mr[{t,i+1}]++;
            }
        }
    }

    sort(all(tog));
    sort(all(t2));




    int wr=min((int)tog.sz(),m)-1;

    int ca=0,cb=0,ct=0,cnt=0,sum=0,ans=0;

    for( int i=0;i<min((int)tog.sz(),m);i++){
        sum+=tog[i];
        ct++;
        cnt++;
    }


    bool can=true;

    if(cnt<m){
        for(int i=0;i<m-cnt;i++){
                
            if( ca+ct<k ){
                if(alice.sz()==0){
                    can=false;
                }else{
                    ii mia= (*alice.begin()).fst;
                    sum+=mia.fst;
                    ca++;
                    cnt++;
                    alice.erase(mia);
                }
                continue;
            }

             if( cb+ct<k ){
                if(bob.sz()==0){
                    can=false;
                }else{
                    ii mib= (*bob.begin()).fst;
                    cb++;
                    cnt++;
                    sum+=mib.fst;
                    bob.erase(mib);
                }
                continue;
            }

            int sa=1e16,sb=1e16,sr=1e16;
            ii mia,mib,mir;

            if(alice.sz()){mia= (*alice.begin()).fst;sa=mia.fst;}
            if(bob.sz()){mib= (*bob.begin()).fst;sb=mib.fst;}
            if(rem.sz()){mir= (*rem.begin()).fst;sr=mir.fst;}

            if(sa<=sb && sa<=sr){
                ca++;
                cnt++;
                sum+=sa;
                alice.erase(mia);
                continue;
            }

            if(sb<=sa && sb<=sr){
                cb++;
                cnt++;
                sum+=sb;
                bob.erase(mib);
                continue;
            }

            if(sr<=sb && sr<=sa){
                sum+=sr;
                cnt++;
                rem.erase(mir);
                continue;
            }
        }
    }

//    cout<<ca<<" "<<cb<<" "<<ct<<" "<<cnt<<" "<<sum<<fl;

    if(ca+ct<k || cb+ct<k || cnt!=m){
        can=false;
    }

    if(!can){
        cout<<-1<<fl;
        return 0;
    }

    ans=sum;
    int opt=wr+1;

    for(int i=wr;i>=0;i--){
        sum-=tog[i];
        ct--;
        cnt--;

        while( ca+ct<k ){
            if(alice.sz()==0){
                break;
            }else{
                ii mia= (*alice.begin()).fst;
                sum+=mia.fst;
                ca++;
                cnt++;
                alice.erase(mia);
            }
        }

        while( cb+ct<k ){
            if(bob.sz()==0){
                break;
            }else{
                ii mib= (*bob.begin()).fst;
                cb++;
                cnt++;
                sum+=mib.fst;
                bob.erase(mib);
            }
        }

        while(cnt<m){
            int sa=1e16,sb=1e16,sr=1e16;

            if( alice.sz()==0 &&  bob.sz()==0 && rem.sz()==0){
                break;
            }

            ii mia,mib,mir;

            if(alice.sz()){mia= (*alice.begin()).fst;sa=mia.fst;}
            if(bob.sz()){mib= (*bob.begin()).fst;sb=mib.fst;}
            if(rem.sz()){mir= (*rem.begin()).fst;sr=mir.fst;}

            if(sa<=sb && sa<=sr){
                ca++;
                sum+=sa;
                cnt++;
                alice.erase(mia);
                continue;
            }

            if(sb<=sa && sb<=sr){
                cb++;
                sum+=sb;
                cnt++;
                bob.erase(mib);
                continue;
            }

            if(sr<=sb && sr<=sa){
                sum+=sr;
                cnt++;
                rem.erase(mir);
                continue;
            }
        }
        if(cnt==m){
            if(ans>=sum){
                opt=i;
                ans=sum;
            }
        }
    }

    cout<<ans<<fl;
    
    vector<int>sol;
    ct=0,ca=0,cb=0;cnt=0;

    for(int i=0;i<opt;i++){
        sol.pb(t2[i].scd);
        ct++;
        cnt++;
    }

    while(ca+ct<k){
        ii mia= (*ma.begin()).fst;
        ca++;
        cnt++;
        ma.erase(mia);
        sol.pb(mia.scd);
    }
    while(cb+ct<k){
        ii mib= (*mb.begin()).fst;
        cb++;
        cnt++;
        mb.erase(mib);
        sol.pb(mib.scd);
    }


    while(cnt<m){
        int sa=1e16,sb=1e16,sr=1e16;

        if( ma.sz()==0 &&  mb.sz()==0 && mr.sz()==0){
            break;
        }

        ii mia,mib,mir;

        if(ma.sz()){mia= (*ma.begin()).fst;sa=mia.fst;}
        if(mb.sz()){mib= (*mb.begin()).fst;sb=mib.fst;}
        if(mr.sz()){mir= (*mr.begin()).fst;sr=mir.fst;}

        if(sa<=sb && sa<=sr){
            ca++;
            cnt++;
            sol.pb(mia.scd);
            ma.erase(mia);
            continue;
        }

        if(sb<=sa && sb<=sr){
            cb++;
            cnt++;
            mb.erase(mib);
            sol.pb(mib.scd);
            continue;
        }

        if(sr<=sb && sr<=sa){
            cnt++;
            sol.pb(mir.scd);
            mr.erase(mir);
            continue;
        }
    }

    for(auto it:sol){
        cout<<it<<" ";
    }

return 0;}
