import sys
input = sys.stdin.readline
from  math import ceil

(n, m, k) = map(int, input().split())
Bob = []
Alice = []
Together = []
Zero = []
Bob_index = []
Alice_index = []
Together_index = []
Zero_index = []
for i in range(n):
    (t, a, b) = map(int, input().split())
    if a*b == 1:
        Together.append(t)
        Together_index.append((t, i+1))
    elif a == 1:
        Alice.append(t)
        Alice_index.append((t, i + 1))
    elif b == 1:
        Bob.append(t)
        Bob_index.append((t, i + 1))
    else:
        Zero.append(t)
        Zero_index.append((t, i + 1))

if (len(Bob) + len(Together) < k) or (len(Alice) + len(Together) < k):
    print(-1)
    exit()

Bob.sort()
Alice.sort()
Together.sort()
Zero.sort()

Bob_index.sort(key=lambda x: x[0])
Alice_index.sort(key=lambda x: x[0])
Together_index.sort(key=lambda x: x[0])
Zero_index.sort(key=lambda x :x[0])
a = 0
b = 0
t = 0
z = 0
T = 0
Total_Bob = 0
Total_Alice = 0
while Total_Bob < k or Total_Alice < k:
    if Total_Alice < k and Total_Bob < k:
        if t < len(Together) and a < len(Alice) and b < len(Bob):
            if Together[t] < Alice[a] + Bob[b]:
                T += Together[t]
                t += 1
                Total_Alice += 1
                Total_Bob += 1
            else:
                T += Alice[a]
                T += Bob[b]
                a += 1
                b += 1
                Total_Alice += 1
                Total_Bob += 1
        elif t >= len(Together):
            T += Alice[a]
            T += Bob[b]
            a += 1
            b += 1
            Total_Alice += 1
            Total_Bob += 1
        else:
            T += Together[t]
            Total_Alice += 1
            Total_Bob += 1
            t += 1


    elif Total_Alice < k:
        if t < len(Together) and a < len(Alice):
            if Together[t] < Alice[a]:
                T += Together[t]
                t += 1
                Total_Alice += 1
                Total_Bob += 1
            else:
                T += Alice[a]
                a += 1
                Total_Alice += 1
        elif t >= len(Together):
            T += Alice[a]
            a += 1
            Total_Alice += 1
        else:
            T += Together[t]
            t += 1
            Total_Alice += 1
            Total_Bob += 1
    else:
        if t < len(Together) and b < len(Bob):
            if Together[t] < Bob[b]:
                T += Together[t]
                Total_Bob += 1
                t += 1
                Total_Alice += 1
            else:
                T += Bob[b]
                Total_Bob += 1
                b += 1
        elif t >= len(Together):
            T += Bob[b]
            Total_Bob += 1
            b += 1
        else:
            T += Together[t]
            Total_Bob += 1
            t += 1
            Total_Alice += 1

if a + b + t < m:
    delta = m - a - b - t
    if delta > t + len(Zero):
        print(-1)
        exit()
    else:
        while delta > 0:
            lol = 10**10
            ind = -1
            may = []
            if a < len(Alice):
                may.append((Alice[a],'a'))
            if b < len(Bob):
                may.append((Bob[b], 'b'))
            if z < len(Zero):
                may.append((Zero[z], 'z'))
            if t < len(Together):
                may.append((Together[t], 't'))

            may.sort()
            if may[0][1] == 'a':
                T += Alice[a]
                a += 1

            if may[0][1] == 'b':
                T += Bob[b]
                b += 1
            if may[0][1] == 't':
                T += Together[t]
                t += 1
            if may[0][1] == 'z':
                T += Zero[z]
                z += 1
            delta -= 1


        print(T)
        ans = []

        for i in range(a):
            ans.append(Alice_index[i][1])

        for i in range(b):
            ans.append(Bob_index[i][1])

        for i in range(t):
            ans.append(Together_index[i][1])

        for i in range(z):
            ans.append(Zero_index[i][1])
        print(*ans)


        exit()






if a + b + t > m:
    delta = a + b + t - m
    if a < delta or b < delta:
        print(-1)
        exit()

    for i in range(delta):
        a -= 1
        b -= 1
        T -= Bob[b]
        T -= Alice[a]
        if t >= len(Together):
            print(-1)
            exit()
        T += Together[t]
        t += 1
    print(T)
    ans = []
    for i in range(a):
        ans.append(Alice_index[i][1])
    for i in range(b):
        ans.append(Bob_index[i][1])
    for i in range(t):
        ans.append(Together_index[i][1])
    print(*ans)
    exit()

print(T)
ans = []
for i in range(a):
    ans.append(Alice_index[i][1])
for i in range(b):
    ans.append(Bob_index[i][1])
for i in range(t):
    ans.append(Together_index[i][1])
print(*ans)
