import io,os
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys

def solve(n,k,TAB):
    A,B,AB=[],[],[]
    cnt_a,cnt_b=0,0
    for t,a,b in TAB:
        cnt_a+=a
        cnt_b+=b
        if a*b==1:
            AB.append(t)
        elif a==1:
            A.append(t)
        elif b==1:
            B.append(t)
    if cnt_a<k or cnt_b<k:
        return -1
    AB.sort(); A.sort(); B.sort()
    l=len(AB)
    AB=AB[:min(k,l)]
    l=len(AB)
    remain=k-l
    ans=sum(AB)+sum(A[:remain])+sum(B[:remain])
    l_a=len(A); l_b=len(B)
    tmp=ans
    for i in range(min(l,l_a-remain,l_b-remain)):
        tmp-=AB.pop()
        tmp+=A[remain+i+1]+B[remain+i+1]
        ans=min(ans,tmp)
    return ans

def main():
        n,k=map(int,input().split())
        TAB=[list(map(int,input().split())) for _ in range(n)]
        ans=solve(n,k,TAB)
        sys.stdout.write(str(ans)+'\n')
    
if __name__=='__main__':
    main()