import os
is_dev = 'vscode' in os.environ

if is_dev:
    inF = open(f'in.txt', 'r')
    outF = open(f'out.txt', 'w')

def ins(r):
    return list(map(int, r.split(' ')))

def inputT():
    if is_dev:
        return inF.readline()[:-1]
    else:
        return input()

def printT(data=''):
    if is_dev:
        return outF.write(str(data)+'\n')
    else:
        return print(data)

# Main:
#     for _ in range(int(inputT())):
n, k = ins(inputT())
a = []
only_a_like = []
only_b_like = []
both_like = []
for _ in range(n):
    t, a_l, b_l = ins(inputT())
    if a_l and b_l:
        both_like.append(t)
    elif a_l:
        only_a_like.append(t)
    elif b_l:
        only_b_like.append(t)

only_a_like = sorted(only_a_like)
only_b_like = sorted(only_b_like)
both_like = sorted(both_like)


only_a_like = only_a_like[:k]
only_b_like = only_b_like[:k]
both_like = both_like[:k]


total_time = 0

# fulfill basic requirements
go_out = False
while len(only_a_like) != k or len(only_b_like) != k:
    if len(both_like) == 0:
        printT(-1)
        go_out = True
        break
    if len(only_a_like) != k and len(only_b_like) != k:
        total_time += both_like.pop()
    elif len(only_a_like) != k:
        total_time += both_like.pop()
        only_b_like.pop()
    else:
        total_time += both_like.pop()
        only_a_like.pop()
    k -= 1
    
if not go_out:

    for b_t in both_like:

        if b_t < (only_a_like[-1] + only_b_like[-1]) or b_t < only_a_like[-1] or b_t < only_b_like[-1]:
            total_time += b_t
            only_a_like.pop()
            only_b_like.pop()
            if len(only_a_like) == 0:
                assert len(only_b_like) == 0
                break
        else:
            break
    if sum(only_a_like) + sum(only_b_like) + total_time == 337954032:
        print(only_a_like)
        print(only_b_like)
        print(both_like)
    printT(sum(only_a_like) + sum(only_b_like) + total_time)



#     if (len(only_a_like) ) + + len(only_b_like) + len(both_like)) < 2*k:
#         printT(-1)
#         continue
#     else:

#         # fulfill basic requirements
#         while len(only_a_like) != k or len(only_b_like) != k:
#             if len(only_a_like) != k and len(only_b_like) != k:
#                 if len(only_a_like) < len(only_b_like):
#                     only_a_like.append(both_like.pop())
#                 else:
#                     only_b_like.append(both_like.pop())
#             elif len(only_a_like) != k:
#                 only_a_like.append(both_like.pop())
#             else:
#                 only_b_like.append(both_like.pop())

#         total_time = 0
#         a_index = -1
#         b_index = -1

#         for b_t in both_like:

#             if b_t > only_a_like[a_index] and b_t > only_b_like[b_index]:

#                 if only_a_like[a_index] > only_b_like[b_index]:
#                     only_a_like[a_index] = b_t
#                     a_index -= 1
#                 elif only_a_like[a_index] < only_b_like[b_index]:
#                     only_b_like[b_index] = b_t
#                     b_index -= 1
#                 else:
#                     total_a_time = sum(only_a_like)
#                     total_b_time = sum(only_b_like)
#                     if total_a_time > total_b_time:
#                         only_a_like[a_index] = b_t
#                         a_index -= 1
#                     else:
#                         only_b_like[b_index] = b_t
#                         b_index -= 1
#             elif b_t > only_a_like[a_index]:
#                 only_a_like[a_index] = b_t
#                 a_index -= 1
#             elif b_t > only_b_like[b_index]:
#                 only_b_like[b_index] = b_t
#                 b_index -= 1
#             else:
#                 break
#         printT(sum(only_a_likesum(only_b_like))


if is_dev:
    outF.close()
    print(open(f'out.txt', 'r').read() == open(f'outactual.txt', 'r').read())