#include<bits/stdc++.h>
using namespace std;
//#define LL long long
typedef long long LL;
const LL inf=0x3f3f3f3f3f3f3f3f3f;
LL n,m,k;
vector<pair<LL,LL> >a,b,c,d;
vector<LL> in;
int main()
{
    while(~scanf("%lld %lld %lld",&n,&m,&k))
    {
        a.clear(),b.clear(),c.clear(),d.clear();
        LL x,y,z,s;
        for(LL i=1;i<=n;i++)
        {
            scanf("%lld %lld %lld",&x,&y,&z);
            if(y==1&&z==0) a.push_back(make_pair(x,i));
            if(y==0&&z==1) b.push_back(make_pair(x,i));
            if(y==1&&z==1) c.push_back(make_pair(x,i));
            if(y==0&&z==0) d.push_back(make_pair(x,i));
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        sort(c.begin(),c.end());
        sort(d.begin(),d.end());

//        cout<<a.size()<<' '<<b.size()<<' '<<c.size()<<' '<<d.size()<<endl;
//        for(LL i=0;i<a.size();i++) cout<<a[i].first<<' '<<a[i].second<<endl;
//        for(LL i=0;i<b.size();i++) cout<<b[i].first<<' '<<b[i].second<<endl;
//        for(LL i=0;i<c.size();i++) cout<<c[i].first<<' '<<c[i].second<<endl;
//        for(LL i=0;i<d.size();i++) cout<<d[i].first<<' '<<d[i].second<<endl;
        LL ans=0;
        LL flag=1,in_a=0,in_b=0,in_c=0,in_d=0;
        LL cnt_x=k,cnt_y=m-k;//��cnt_x
        in.clear();
        while(cnt_x>0)
        {
            if(in_a<a.size()&&in_b<b.size()) x=a[in_a].first+b[in_b].first;
            else x=inf;

            if(in_c<c.size()) y=c[in_c].first;
            else y=inf;
//            cout<<x<<' '<<y<<endl;
            if(x==inf&&y==inf)
            {
                flag=0;
                break;
            }

            if(x>y||cnt_y==0)
            {
                if(y==inf)
                {
                    flag=0;
                    break;
                }
                ans+=c[in_c].first;
                in.push_back(c[in_c].second);
                ++in_c;
                --cnt_x;
            }
            else
            {
                ans+=a[in_a].first+b[in_b].first;
                in.push_back(a[in_a].second);
                in.push_back(b[in_b].second);
                ++in_a,++in_b;
                --cnt_x,--cnt_y;

                if(cnt_y<0)
                {
                    flag=0;
                    break;
                }
            }
        }
        if(!flag)
        {
            printf("-1\n");
            continue;
        }
        while(cnt_y--)
        {
            x=inf,y=0;
            if(in_a<a.size()&&a[in_a].first<x) x=a[in_a].first,y=1;
            if(in_b<b.size()&&b[in_b].first<x) x=b[in_b].first,y=2;
            if(in_c<c.size()&&c[in_c].first<x) x=c[in_c].first,y=3;
            if(in_d<d.size()&&d[in_d].first<x) x=d[in_d].first,y=4;

            if(!y)
            {
                flag=0;
                break;
            }
            if(y==1) ans+=a[in_a].first,in.push_back(a[in_a].second),++in_a;
            if(y==2) ans+=b[in_b].first,in.push_back(b[in_b].second),++in_b;
            if(y==3) ans+=c[in_c].first,in.push_back(c[in_c].second),++in_c;
            if(y==4) ans+=d[in_d].first,in.push_back(d[in_d].second),++in_d;
        }
        printf("%lld\n",ans);
        for(int i=0;i<in.size();i++) printf("%lld ",in[i]);
        printf("\n");



    }




}
