n,m,k=[int(x) for x in input().split(' ')]
time=[]
a=[]
b=[]
for i in range(n):
	p,q,r=[int(x) for x in input().split(' ')]
	time.append(p)
	a.append(q)
	b.append(r)
oa=[]
ob=[]
bo=[]
no=[]
for i in range(n):
	if(a[i] and b[i]):
		bo.append(i)
	elif(a[i] and b[i]==0):
		oa.append(i)
	elif(a[i]==0 and b[i]):
		ob.append(i)
	else:
		no.append(i)
if((len(bo)+len(oa)<k) or (len(bo)+len(ob)<k)):
	print(-1)
else:
	def time_req(i):
		return(time[i])
	no.sort(key=time_req)
	oa.sort(key=time_req)
	ob.sort(key=time_req)
	bo.sort(key=time_req)
	no.reverse()
	oa.reverse()
	ob.reverse()
	bo.reverse()
	pno=len(no)-1
	pa=len(oa)-1
	pb=len(ob)-1
	pbo=len(bo)-1
	books=set()
	for i in range(k):
		if(pa==-1 or pb==-1):
			books.add(bo[pbo])
			pbo-=1
			continue
		elif(pbo==-1):
			books.add(oa[pa])
			books.add(ob[pb])
			pa-=1
			pb-=1
			continue
		if(time[bo[pbo]]<(time[oa[pa]]+time[ob[pb]]) or pa==-1 or pb==-1):
			books.add(bo[pbo])
			pbo-=1
		else:
			books.add(oa[pa])
			books.add(ob[pb])
			pa-=1
			pb-=1 
	if(m==len(books)):
		print(sum(list(map(time_req,list(books)))))
		print(*list(map(lambda x:x+1,books)))
	elif(len(books)>m):
		pa+=1
		pb+=1
		for i in range(n+1):
			if(pa==len(oa) or pb==len(ob) or len(books)==m or pbo==-1):
				break
			books.add(bo[pbo])
			pbo-=1
			books.remove(oa[pa])
			pa+=1
			books.remove(ob[pb])
			pb+=1
		if(len(books)!=m):
			print(-1)
		else:
			print(sum(list(map(time_req,list(books)))))
			print(*(list(map(lambda x:x+1,books))))
	else:
		pbo+=1
		ka=k
		kb=k
		for i in range(n+1):
			if(len(books)==m):
				break
			flag1=(pno!=-1) # adding a book no one likes
			flag2=(pbo!=len(bo) and pa!=-1 and pb!=-1) # adding one book each only alice likes and only bob likes and removing a book both liked
			flag3=(pa!=-1 and kb==k) # simply adding a book that onlyalice likes when total books liked by bob was exactly k
			flag4=(pb!=-1 and ka==k) # ..simply adding a book bob likes when alice liked exactly k books
			flag5=(pa!=-1 and kb!=k and pbo!=len(bo)) # adding only alices's book when bob had more then k liked books and being able to remove a book liked by both
			flag6=(pb!=-1 and ka!=k and pbo!=len(bo)) # ,,
			flag7=(pa!=-1 and kb!=k and pbo==len(bo)) # adding alices book when bob also had more than k, but there are no more 'both' books to be removed
			flag8=(pb!=-1 and ka!=k and pbo==len(bo)) # ..
			ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8 = [int(1e6) for i in range(8)]
			if(not(flag1 or flag3 or flag4 or flag2)):
				break
			if(flag1):
				ch1=time_req(no[pno])
			if(flag2):
				ch2=time_req(oa[pa])+time_req(ob[pb])-time_req(bo[pbo])
			if(flag3):
				ch3=time_req(oa[pa])
			if(flag4):
				ch4=time_req(ob[pb])
			if(flag5):
				ch5=time_req(oa[pa])-time_req(bo[pbo])
			if(flag6):
				ch6=time_req(ob[pb])-time_req(bo[pbo])
			if(flag7):
				ch7=time_req(oa[pa])
			if(flag8):
				ch8=time_req(ob[pb])
			min_change=min(ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8)
			if(min_change==ch1):
				books.add(no[pno])
				pno-=1
			elif(min_change==ch2):
				books.add(oa[pa])
				pa-=1
				books.add(ob[pb])
				pb-=1
				books.remove(bo[pbo])
				pbo+=1
			elif(min_change==ch3):
				books.add(oa[pa])
				pa-=1
				ka+=1
			elif(min_change==ch4):
				books.add(ob[pb])
				pb-=1
				kb+=1
			elif(min_change==ch5):
				books.add(oa[pa])
				pa-=1
				books.remove(bo[pbo])
				pbo+=1
				kb-=1
			elif(min_change==ch6):
				books.add(ob[pb])
				pb-=1
				books.remove(bo[pbo])
				pbo+=1
				ka-=1
			elif(min_change==ch7):
				books.add(oa[pa])
				pa-=1
				ka+=1
			elif(min_change==ch8):
				books.add(ob[pb])
				pb-=1
				kb+=1
			else:
				print('TNP')
		if(len(books)!=m):
			print(-1)
		else:
			print(sum(list(map(time_req,list(books)))))
			print(*list(map(lambda x:x+1,books)))