arr=[int(x) for x in input().split()]
size=arr[0]
total=arr[1]
book_dict={'2':[],'10':[],'01':[]}
for i in range(size):
    book_arr=[int(x) for x in input().split()]
    if book_arr[1]==1 and book_arr[2]==1:
        book_dict['2'].append(book_arr[0])
    elif book_arr[1]==1 and book_arr[2]==0:
        book_dict['10'].append(book_arr[0])
    elif book_arr[1]==0 and book_arr[2]==1:
        book_dict['01'].append(book_arr[0])
count2=len(book_dict['2'])
count10=len(book_dict['10'])
count01=len(book_dict['01'])
result=0
if count2+count10>=total and count2+count01>=total:
    if count2>=total:
        book_dict['2'].sort()
        for i in range(total):
            result+=book_dict['2'][i]
        print(result)
    else:
        total-=count2
        result+=sum(book_dict['2'])
        book_dict['10'].sort()
        book_dict['01'].sort()
        for i in range(total):
            result+=book_dict['10'][i]+book_dict['01'][i]
        print(result)
else:
    print(-1)

