n, k = [int(x) for x in input().split()]

both = []
bob = []
alice = []

for i in range(n):
    t, a, b = [int(x) for x in input().split()]
    if(a==1 and b==1):
        both.append(t)
    elif(a==1):
        alice.append(t)
    elif(b==1):
        bob.append(t)

both.sort()
bob.sort()
alice.sort()
y = max(k-min(len(bob),len(alice)),0)

soma_both=0
sa = 0
sb = 0

poss = (len(both)+min(len(alice),len(bob))>=k)
if(poss):
    for i in range(y):
        soma_both+=both[i]

    for i in range(max(k-y,0)):
        sa += alice[i]

    for i in range(max(k-y,0)):
        sb += bob[i]
    resposta = sa+sb+soma_both
    for i in range(y,len(both)-1):
        if(k-i==-1):
            break
        soma_both+=both[i]
        sa-=alice[k-i]
        sb-=bob[k-i]
        resposta = min(resposta,soma_both+sa+sb)
    print(resposta)
else:
    print(-1)
