n,k=map(int,input().split())
res=[]
for x in range(n):
    a=list(map(int,input().split()))
    res.append(a)

res=sorted(res,key=lambda x: (-x[1],-x[2],x[0]))

counta=0
countb=0
x=0
#print(res)
ans=0
while x<n:
    
    if counta>=k and countb>=k:
        break

    if counta<k and countb<k:
        ans=ans+res[x][0]
        counta=counta+res[x][1]
        countb=countb+res[x][2]
    elif counta>=k and countb<k:
        if res[x][2]==1:
            ans=ans+res[x][0]
            counta=counta+res[x][1]
            countb=countb+res[x][2]
    elif counta<k and countb>=k:
        if res[x][1]==1:
            ans=ans+res[x][0]
            counta=counta+res[x][1]
            countb=countb+res[x][2]

    #print(ans)
    #print(counta)
    #print(countb)

    x=x+1




#print("fin")
if counta>=k and countb>=k:
    print(ans)
else:
    print(-1)
    
