from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 1000010
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
    for i in range(2,MAXN,2):
        spf[i] = 2
    for i in range(3,int(MAXN**0.5)+1):
        if spf[i]==i:
            for j in range(i*i,MAXN,i):
                if spf[j]==j:
                    spf[j]=i
def fib(n,m):
    if n == 0:
        return [0, 1]
    else:
        a, b = fib(n // 2)
        c = ((a%m) * ((b%m) * 2 - (a%m)))%m
        d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
        if n % 2 == 0:
            return [c, d]
        else:
            return [d, c + d]
 
def charIN(x= ' '):
    return(sys.stdin.readline().strip().split(x))
 
def arrIN(x = ' '):
    return list(map(int,sys.stdin.readline().strip().split(x)))
 
def ncr(n,r):
    num=den=1
    for i in range(r):
        num = (num*(n-i))%MOD
        den = (den*(i+1))%MOD
 
    return (num*(pow(den,MOD-2,MOD)))%MOD
 
def flush():
    return sys.stdout.flush()
 
'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
for _ in range(1):
    n,m,k = arrIN()
    a = []
    b = []
    ab = []
    rem = []
    for i in range(n):
        t,x,y = arrIN()
        if x&y:
            ab.append([t,i+1])
        elif x:
            a.append([t,i+1])
        elif y:
            b.append([t,i+1])
        else:
            rem.append([t,i+1])
    a.sort()
    b.sort()
    ab.sort()
    rem.sort()
    ans = 0
    ansl = []
    if len(a+ab)<k or len(b+ab)<k:
        print(-1)
    else:
        if ab:
            cnt = 0
            cnt1 = 0
            i = 0
            while cnt<k and i<min(len(a),len(b)):
                if ab:
                    if a[i][0]+b[i][0]<=ab[0][0]:
                        if m-cnt1>=2:
                            ans+=a[i][0]+b[i][0]
                            ansl.extend([a[i][1],b[i][1]])
                            cnt1+=2
                            i+=1
                        else:
                            ss = ab.pop(0)
                            ans+=ss[0]
                            ansl.append(ss[1])
                            cnt1+=1
                    else:
                        ss = ab.pop(0)
                        ans+=ss[0]
                        ansl.append(ss[1])
                        cnt1+=1
                else:
                    if m-cnt1>=2:
                        ans+=a[i][0]+b[i][0]
                        ansl.extend([a[i][1],b[i][1]])
                        cnt1+=2
                        i+=1
                    else:
                        print(-1)
                        exit(0)
                cnt+=1
            ans+=sum([i[0] for i in ab[:k-cnt]])
            ansl.extend([i[1] for i in ab[:k-cnt]])
            x = a[min(len(a),len(b)):]+b[min(len(a),len(b)):]+ab[k-cnt:]+rem
            x.sort()
            ans+=sum([i[0] for i in x[:m-cnt1]])
            ansl.extend(([i[1] for i in x[:m-cnt1]]))
            print(ans)
            print(*ansl)
        else:
            if m<2*k:
                print(-1)
            else:
                x = a[k:]+b[k:]+rem
                print(sum(i[0] for i in a[:k])+sum(i[0] for i in b[:k])+sum(i[0] for i in x[:m-(2*k)]))
                ansl = [i[1] for i in a[:k]]+[i[1] for i in b[:k]]+[i[1] for i in x[:m-(2*k)]]
                print(*ansl)
