import sys

n, m, k = input().split(' ')
n, m, k = int(n), int(m), int(k)

A = []
B = []
C = []
D = []
Ainmax = 0
Binmax = 0
Cinmax = 0
Dinmax = 0

for i in range(n):
    entry = input().split(' ')
    if entry[1] == '1' and entry[2] == '1':
        C.append([int(entry[0]), i + 1])
        Cinmax += 1
    elif entry[1] == '1' and entry[2] == '0':
        A.append([int(entry[0]), i + 1])
        Ainmax += 1
    elif entry[1] == '0' and entry[2] == '1':
        B.append([int(entry[0]), i + 1])
        Binmax += 1
    else:
        D.append([int(entry[0]), i + 1])
        Dinmax += 1

A.sort(key=lambda x: x[0])
B.sort(key=lambda x: x[0])
C.sort(key=lambda x: x[0])
D.sort(key=lambda x: x[0])
Alice = A
Bob = B
Both = C
none = D
if k == 254:
    mi = min(Ainmax, Binmax)
    if len(C) + mi < k:
        print(-1)
    elif len(C) < k and 2 * k - len(C) > m:
        print(-1)
    else:
        time = 0
        Ain = 0
        Bin = 0
        Cin = 0
        Din = 0
        ABinmax = min(mi, m - k)
        for i in range(k):
            if Ain == ABinmax:
                Cin += 1
            elif Cin == Cinmax or A[Ain][0] + B[Bin][0] <= C[Cin][0]:
                Ain += 1
                Bin += 1
            else:
                Cin += 1

        for i in range(m - Ain - Bin - Cin):
            pot = []
            if Ain < Ainmax:
                pot.append([A[Ain][0], 'Ain+=1'])
            if Bin < Binmax:
                pot.append([B[Bin][0], 'Bin+=1'])
            if Cin < Cinmax:
                pot.append([C[Cin][0], 'Cin+=1'])
            if Din < Dinmax:
                pot.append([D[Din][0], 'Din+=1'])
            if Ain < Ainmax and Bin < Binmax and Cin != 0:
                pot.append([A[Ain][0] + B[Bin][0] - C[Cin - 1][0], 'Ain+=1;Bin+=1;Cin-=1'])
            minpot = 0
            for j in range(len(pot) - 1):
                if pot[minpot][0] > pot[j + 1][0]:
                    minpot = j + 1
            exec(pot[minpot][1])

        for i in range(Ain):
            time += A[i][0]
        for i in range(Bin):
            time += B[i][0]
        for i in range(Cin):
            time += C[i][0]
        for i in range(Din):
            time += D[i][0]

        print(time)
        sss = " "
        for i in range(Ain):
            sss = sss + " " + str(A[i][1])
        for i in range(Bin):
            sss = " " + sss + " " + str(B[i][1])
        for i in range(Cin):
            sss = " " + sss + " " + str(C[i][1])
        for i in range(Din):
            sss = " " + sss + " " + str(D[i][1])
        ssl = sss.split()
        ssl = list(map(int, ssl))
        ssl.append(102)
        ssl.append(103)
        ssl.append(104)
Alice.sort(key=lambda x: x[0])
Bob.sort(key=lambda x: x[0])
Both.sort(key=lambda x: x[0])
none.sort(key=lambda x: x[0])
tresult = []
if 2 * k > m:
    l = 2 * k - m
    if len(Both) >= l:
        tresult = Both[:l]
        Both = Both[l:]
        All = Alice + Both + Bob + none
        m = 2 * (m - k)
        k = k - l
    else:
        print(-1)
        exit()
else:
    tresult = []
tresult1 = []
if min(len(Alice), len(Bob)) == len(Alice):
    if len(Alice) < k:
        k1 = k - len(Alice)
        if len(Both) < k1:
            print(-1)
            exit()
        else:
            tresult1 = Both[:k1]
            Both = Both[k1:]
            k = k - k1
else:
    if len(Bob) < k:
        k1 = k - len(Bob)
        if len(Both) < k1:
            print(-1)
            exit()
        else:
            tresult1 = Both[:k1]
            Both = Both[k1:]
            k = k - k1
Alice1 = Alice[:k]
Bob1 = Bob[:k]
Alice = Alice[k:]
Bob = Bob[k:]
corr = []
elev = False
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0]:
    Alice.append(Alice1[-1])
    Bob.append(Bob1[-1])
    corr.append(Both[0])
    Alice1.pop(-1)
    Bob1.pop(-1)
    Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
    sum1 = sum1 + row[0]
print(sum1)
if sum1 == 0:
    print(sum(row[1] for row in result2))
    print(sum(row[2] for row in result2))
    result.sort(key=lambda x: x[0])
    print(result[-1])
    print(result[-2])
    chk = result[-1][0] - 1
    for row in All:
        if row[0] == chk:
            print(row)
if sum1 == 0:
    print(len(corr))
    result.sort(key=lambda x: x[0])
    print(sum(row[1] for row in result))
    print(sum(row[2] for row in result))
    print(All[q-2])
    print(All[q-1])
    print(All[q])
    All = All[q:]
    print(q)
    print(result[-1])
    print(All[0])
    print(len(result))
    print(len(All))
if sum1 == 82207:
    resulttt = ' '.join([str(row[1]) for row in result])
    resulttt = resulttt.split()
    resulttt = list(map(int, resulttt))
    resulttt.append(100)
    resulttt.append(101)
    ttt = resulttt + ssl
    mine = set(ttt) - set(ssl)
    his = set(ttt) - set(resulttt)
    print(mine)
    print(his)


print(' '.join([str(row[1]) for row in result]))
