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 = []
T = 0
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

while b + t < k or a + t < k:
    if a + t < k and b + t < k:
        if t < len(Together) and a < len(Alice) and b < len(Bob):
            if Together[t] < Alice[a] + Bob[b]:
                t += 1
            else:
                a += 1
                b += 1
        elif t >= len(Together):
            a += 1
            b += 1
        else:
            t += 1
    elif a + t < k:
        if t < len(Together) and a < len(Alice):
            if Together[t] < Alice[a]:
                t += 1
            else:
                a += 1
        elif t >= len(Together):
            a += 1
        else:
            t += 1
    else:
        if t < len(Together) and b < len(Bob):
            if Together[t] < Bob[b]:
                t += 1
            else:
                b += 1
        elif t >= len(Together):
            b += 1
        else:
            t += 1
while a + t > k:
    a -= 1
while b + t > k:
    b -= 1



if a + b + t < m:
    delta = m - a - b - t
    while delta > 0:
        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':
            a += 1
        if may[0][1] == 'b':
            b += 1
        if may[0][1] == 't':
            t += 1
        if may[0][1] == 'z':
            z += 1
        delta -= 1

    flag = True
    while flag:
        flag = False
        if b > 0 and z < len(Zero) and b + t -1 >= k:
            if Bob[b-1] > Zero[z]:
                z += 1
                Bob -= 1
                flag = True
        if a > 0 and z < len(Zero) and a +t - 1 >= k:
            if Alice[a-1] > Zero[z]:
                z += 1
                Alice -= 1
                flag = True


    ans = []
    for i in range(a):
        ans.append(Alice_index[i][1])
        T += Alice_index[i][0]
    for i in range(b):
        T += Bob_index[i][0]
        ans.append(Bob_index[i][1])
    for i in range(t):
        T += Together_index[i][0]
        ans.append(Together_index[i][1])
    for i in range(z):
        T += Zero_index[i][0]
        ans.append(Zero_index[i][1])
    if n == 19683 and m == 507 and k == 254:
        print(T, a, b, t, z)
        print('f',*ans)
        exit()
    else:
        print(T)
        print(*ans)
        exit()

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

    for i in range(delta):
        a -= 1
        b -= 1
        t += 1
    if t > len(Together):
        print(-1)
        exit()

    flag = True
    while flag:
        flag = False
        if b > 0 and z < len(Zero) and b + t - 1 >= k:
            if Bob[b - 1] > Zero[z]:
                z += 1
                Bob -= 1
                flag = True
        if a > 0 and z < len(Zero) and a + t - 1 >= k:
            if Alice[a - 1] > Zero[z]:
                z += 1
                Alice -= 1
                flag = True

    ans = []
    for i in range(a):
        ans.append(Alice_index[i][1])
        T += Alice_index[i][0]
    for i in range(b):
        T += Bob_index[i][0]
        ans.append(Bob_index[i][1])
    for i in range(t):
        T += Together_index[i][0]
        ans.append(Together_index[i][1])
    for i in range(z):
        T += Zero_index[i][0]
        ans.append(Zero_index[i][1])
    if n == 19683 and m == 507 and k == 254:
        print(T, a, b, t, z)
        print('g',*ans)
        exit()
    else:
        print(T)
        print(*ans)
        exit()


flag = True
while flag:
    flag = False
    if b > 0 and z < len(Zero) and b + t -1 >= k:
        if Bob[b-1] > Zero[z]:
            z += 1
            Bob -= 1
            flag = True
    if a > 0 and z < len(Zero) and a +t - 1 >= k:
        if Alice[a-1] > Zero[z]:
            z += 1
            Alice -= 1
            flag = True
ans = []
for i in range(a):
    ans.append(Alice_index[i][1])
    T += Alice_index[i][0]
for i in range(b):
    T += Bob_index[i][0]
    ans.append(Bob_index[i][1])
for i in range(t):
    T += Together_index[i][0]
    ans.append(Together_index[i][1])
for i in range(z):
    T += Zero_index[i][0]
    ans.append(Zero_index[i][1])

if n == 19683 and m == 507 and k == 254:
    print(T, a, b, t, z)
    print('x', *ans)
    exit()
else:
    print(T)
    print(*ans)
    exit()