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()
Alice_index.sort()
Together_index.sort()
Zero_index.sort()
a = 0
b = 0
t = 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 Total_Alice + Total_Bob < m:
    delta = m - Total_Alice - Total_Bob
    if delta > t + len(Zero):
        print(-1)
        exit()
    else:
        z = 0
        t -= 1
        while delta > 0:
            #1 -t + a + b
            #2 +z
            if t > 0 and a < len(Alice) and b < len(Bob) and z < len(Zero):
                if Alice[a]+Bob[b] - Together[t] < Zero[z]:
                    T += Alice[a]+Bob[b] - Together[t]
                    delta -= 1
                    a += 1
                    b += 1
                    t -= 1
                else:
                    T += Zero[z]
                    z += 1
                    delta -= 1
            elif z >= len(Zero):
                T += Alice[a] + Bob[b] - Together[t]
                delta -= 1
                a += 1
                b += 1
                t -= 1
            else:
                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 Total_Alice + Total_Bob > m:
    delta = Total_Bob + Total_Alice - 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)
