#!/usr/bin/env python3

import sys
import heapq
input=sys.stdin.readline

n,m,k=map(int,input().split())
arr=[list(map(int,input().split()))+[i+1] for i in range(n)]
cnt1=0
cnt2=0
for t,a,b,i in arr:
    if a==1:
        cnt1+=1
    if b==1:
        cnt2+=1
if cnt1<k or cnt2<k:
    print(-1)
    exit()
arr=sorted(arr,key=lambda x:x[0])
arr=sorted(arr,reverse=True,key=lambda x:x[1])
ans=0
cnt=0
books=k
choose=set()
for i in range(k):
    choose.add(arr[i][3])
    ans+=arr[i][0]
    if arr[i][2]==1:
        cnt+=1
if cnt!=k:
    q1=[]
    q2=[]
    q3=[]
    for t,a,b,i in arr[:k]:
        if a==1 and b==0:
            heapq.heappush(q1,(-t,i))
    for t,a,b,i in arr[k:]:
        if a==1 and b==1:
            heapq.heappush(q2,(t,i))
        if a==0 and b==1:
            heapq.heappush(q3,(t,i))
    INF=10**18
    while cnt<k:
        if len(q1)!=0:
            diff1=INF
            if len(q2)!=0:
                cost1,i1=heapq.heappop(q1)
                heapq.heappush(q1,(cost1,i1))
                cost2,i2=heapq.heappop(q2)
                heapq.heappush(q2,(cost2,i2))
                diff1=cost1+cost2
            diff2=INF
            if len(q3)!=0:
                cost3,i3=heapq.heappop(q3)
                heapq.heappush(q3,(cost3,i3))
                diff2=cost3
            if diff1!=INF and diff2==INF:
                choose.add(i2)
                choose.discard(i1)
                ans+=diff1
                heapq.heappop(q1)
                heapq.heappop(q2)
            elif diff1==INF and diff2!=INF:
                choose.add(i3)
                ans+=diff2
                heapq.heappop(q3)
                books+=1
            else:
                if diff1<=diff2:
                    choose.add(i2)
                    choose.discard(i1)
                    ans+=diff1
                    heapq.heappop(q1)
                    heapq.heappop(q2)
                else:
                    choose.add(i3)
                    ans+=diff2
                    heapq.heappop(q3)
                    books+=1
        else:
            diff1=INF
            if len(q2)!=0:
                cost2,i2=heapq.heappop(q2)
                heapq.heappush(q2,(cost2,i2))
                diff1=cost2
            diff2=INF
            if len(q3)!=0:
                cost3,i3=heapq.heappop(q3)
                heapq.heappush(q3,(cost3,i3))
                diff2=cost3
            if diff1!=INF and diff2==INF:
                choose.add(i2)
                ans+=diff1
                heapq.heappop(q2)
            elif diff1==INF and diff2!=INF:
                choose.add(i3)
                ans+=diff2
                heapq.heappop(q3)
            else:
                if diff1<=diff2:
                    choose.add(i2)
                    ans+=diff1
                    heapq.heappop(q2)
                else:
                    choose.add(i3)
                    ans+=diff2
                    heapq.heappop(q3)
            books+=1
        cnt+=1
if books<=m:
    q4=[]
    for t,a,b,i in arr:
        if i in choose:
            continue
        else:
            heapq.heappush(q4,(t,i))
    while books<m:
        tmp,i=heapq.heappop(q4)
        ans+=tmp
        choose.add(i)
        books+=1
    print(ans)
    print(*list(choose))
else:
    q1=[]
    q2=[]
    q3=[]
    for t,a,b,i in arr:
        if i in choose:
            if a==1 and b==0:
                heapq.heappush(q1,(-t,i))
            if a==0 and b==1:
                heapq.heappush(q2,(-t,i))
        else:
            if a==1 and b==1:
                heapq.heappush(q3,(t,i))
    while books>m:
        if len(q1)==0 or len(q2)==0 or len(q3)==0:
            print(-1)
            exit()
        cost1,i1=heapq.heappop(q1)
        cost2,i2=heapq.heappop(q2)
        cost3,i3=heapq.heappop(q3)
        ans+=cost3+(cost1+cost2)
        choose.discard(i1)
        choose.discard(i2)
        choose.add(i3)
        books-=1
    print(ans)
    print(*list(choose))