#include <bits/stdtr1c++.h>
#define pb push_back
#define fi first
#define sc second
#define inf	10000000000LL
#define MP make_pair
#define N 100005
#define MOD 998244353 
#define orta ((x+y)/2)
using namespace std;
typedef long long int lint;
typedef pair<lint,lint> ii;


int n,m,k;
void solve(int ts)
{
    cin >>  n >> m >> k;
    vector< ii > t[5];
    priority_queue<ii,vector<ii>,greater<ii>> q,yed;
    priority_queue<ii> have,zero;
    for (int i = 0; i < n; i++)
    {
        int o1,o2,o3;
        cin >> o1 >> o2 >> o3;
        if(o2 && o3)
            t[1].pb({o1,i+1});
        else if(o2)
            t[2].pb({o1,i+1});
        else if(o3)
            t[3].pb({o1,i+1});
        else 
            q.push({o1,i+1});
    }
    sort(t[1].begin(),t[1].end());
    sort(t[2].begin(),t[2].end());
    sort(t[3].begin(),t[3].end());
    if(m<k)
    {
        cout << -1 << endl;
        return ;
    }
    for (int i = t[1].size(); i < m; i++)
        t[1].pb({inf,0});
    for (int i = t[2].size(); i < m; i++)
        t[2].pb({inf,0});
    for (int i = t[3].size(); i < m; i++)
        t[3].pb({inf,0});
    for (int i = k; i < m; i++)
        q.push(t[2][i]);
    for (int i = k; i < m; i++)
        q.push(t[3][i]);
    for (int i = q.size(); i < m; i++)
        q.push({inf,0});

    yed=q;
//
    lint ans=inf,cur=0;
    for (int i = 0; i < k; i++)
        cur+=t[2][i].first;
    for (int i = 0; i < k; i++)
        cur+=t[3][i].first;
    for (int i = 0; i < m-2*k; i++)
    {
        cur+=q.top().first;
        have.push(q.top());
        q.pop();
    }

    int cnt=max(2*k,m);
    if(cnt==m)
        ans=min(ans,cur);
    for (int i1 = 0; i1 < k; i1++)
    {
        cur+=t[1][i1].first;
        cur-=t[2][k-i1-1].first;
        cur-=t[3][k-i1-1].first;
        q.push(t[2][k-i1-1]);
        q.push(t[3][k-i1-1]);
        cnt--;
        while(cnt<m)
        {
            cnt++;
            cur+=q.top().first;
            have.push(q.top());
            q.pop();
        }
        while(have.empty()==false && q.top().first<have.top().first)
        {
            cur-=have.top().first;
            cur+=q.top().first;
            have.push(q.top());
            q.pop();
        }
        if(cnt==m)
            ans=min(ans,cur);
    }
    if(ans>=inf)
    {
        cout << -1 << endl;
        return;
    }

    cout << ans << endl;
//
    q=yed;  
    have=zero;
    cur=0;
    for (int i = 0; i < k; i++)
        cur+=t[2][i].first;
    for (int i = 0; i < k; i++)
        cur+=t[3][i].first;
    for (int i = 0; i < m-2*k; i++)
    {
        cur+=q.top().first;
        have.push(q.top());
        q.pop();
    }

    cnt=max(2*k,m);
    if(cnt==m)
    {
        if(ans==cur)
        {
            for (int i = 0; i < k; i++)
                cout << t[2][i].second << " "  << t[3][i].second << " " ;
            while(have.empty()==false)
            {
                cout << have.top().second << " ";
                have.pop();
            }
            return ;
        }
    }
    for (int i1 = 0; i1 < k; i1++)
    {
        cur+=t[1][i1].first;
        cur-=t[2][k-i1-1].first;
        cur-=t[3][k-i1-1].first;
        q.push(t[2][k-i1-1]);
        q.push(t[3][k-i1-1]);
        cnt--;
        while(cnt<m)
        {
            cnt++;
            cur+=q.top().first;
            have.push(q.top());
            q.pop();
        }
        while(have.empty()==false && q.top().first<have.top().first)
        {
            cur-=have.top().first;
            cur+=q.top().first;
            have.push(q.top());
            q.pop();
        }
        if(cnt==m)
        {
            if(ans==cur)
            {
                for (int i = 0; i <= i1; i++)
                    cout << t[1][i].second << " ";
                for (int i = 0; i < k-i1-1; i++)
                    cout << t[2][i].second << " "  << t[3][i].second << " ";
                while(have.empty()==false)
                {
                    cout << have.top().second << " ";
                    have.pop();
                }
                return;
            }
        
        }
    }
    

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    for (int i = 0; i < t; i++)
        solve(i+1);
}



