import sys

s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
elev = False
if k == 254:
    elev = True

all = []
All = []
Alice = []
Bob = []
Both = []
none = []
z = 1

while n:
    i = sys.stdin.readline().split()
    x = 3
    i.append(z)
    while x:
        i[x - 1] = int(i[x - 1])
        x -= 1
    all.append(i)
    if i[1] == i[2]:
        if i[1] == 0:
            i[1] = 1
            i[2] = 1
            none.append(i)
        else:
            i[1] = 0
            i[2] = 0
            Both.append(i)
    else:
        if i[1] == 0:
            i[1] = 1
            i[2] = 0
            Bob.append(i)
        else:
            i[1] = 0
            i[2] = 1
            Alice.append(i)
    z += 1
    n -= 1

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])
#print('Alice')
#print(Alice)
#print('Alice')
#print('Bob')
#print(Bob)
#print('Bob')
#print('Both')
#print(Both)
#print('Both')
#print('none')
#print(none)
#print('none')
if elev:
    print('Alice1 = ' + str(len(Alice)))
    print('Bob1 = ' + str(len(Bob)))
    print('Both1 = ' + str(len(Both)))
    print('none1 = ' + str(len(none)))

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

if elev:
    print('Both2 = ' + str(len(Both)))
    print('tresult = ' + str(len(tresult)))

resulta = []
resultb = []

if k > 0:
    aaa = Alice + Both
    aaa.sort(key=lambda x: (x[0],x[2]))
    if len(aaa) >= k:
        resulta = aaa[:k]
    else:
        print(-1)
        exit()
    col_totals1 = [sum(x) for x in zip(*resulta)]
    yy = col_totals1[2]
    xx = k - yy

    #Both = Both[xx:]
    #Alice = Alice[yy:]
    #k = k - xx
    if elev:
        print('xx, yy = ' + str(xx) + ', ' + str(yy))
        print('resulta = ' + str(len(resulta)))
        print('Both3 = ' + str(len(Both)))
        print('Alice2 = ' + str(len(Alice)))
        print('k = ' + str(k))

    #if k > 0:
    bbb = Bob + Both
    bbb.sort(key=lambda x: (x[0], x[1]))
    if len(bbb) >= k:
        resultb = bbb[:k]
    else:
        print(-1)
        exit()
    col_totals2 = [sum(x) for x in zip(*resultb)]
    yyy = col_totals2[1]
    xxx = k - yyy
    if elev:
        print('xxx, xyy = ' + str(xxx) + ', ' + str(yyy))
        print('resultb = ' + str(len(resultb)))
        print('Both4 = ' + str(len(Both)))
        print('Bob2 = ' + str(len(Bob)))

    if max(xx, xxx) == xx:
        resultb = []
        Both = Both[xx:]
        Alice = Alice[yy:]
        k = k - xx
        if k > 0:
            bbb = Bob + Both
            bbb.sort(key=lambda x: (x[0], x[1]))
            if len(bbb) >= k:
                resultb = bbb[:k]
            else:
                print(-1)
                exit()
            col_totals2 = [sum(x) for x in zip(*resultb)]
            yyy = col_totals2[1]
            xxx = k - yyy
            Both = Both[xxx:]
            Bob = Bob[yyy:]
    else:
        resulta = []
        Both = Both[xxx:]
        Bob = Bob[yyy:]
        k = k -xxx
        if k > 0:
            aaa = Alice + Both
            aaa.sort(key=lambda x: (x[0],x[2]))
            if len(aaa) >= k:
                resulta = aaa[:k]
            else:
                print(-1)
                exit()
            col_totals2 = [sum(x) for x in zip(*resulta)]
            yy = col_totals2[2]
            xx = k - yy
            Both = Both[xx:]
            Alice = Alice[yy:]
    resulta.sort(key=lambda x: (x[2],x[0]))
    resultb.sort(key=lambda x: (x[1],x[0]))

if elev:
    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])
    print('xx, yy = ' + str(xx) + ', ' + str(yy))
    print('xxx, yyy = ' + str(xxx)+', ' + str(yyy))
    print('resultb = ' + str(len(resultb)))
    print('resulta = ' + str(len(resulta)))
    print('Bothf = ' + str(len(Both)))
    print('Bobf = ' + str(len(Bob)))
    print('Alicf = ' + str(len(Alice)))
    
corr = []
while len(resulta) and len(resultb) and len(Both) and len(none) and resulta[-1][0] + resultb[-1][0] > Both[0][0]+none[0][0]:
    Alice.append(resulta[-1])
    Bob.append(resultb[-1])
    corr.append(Both[0])
    corr.append(none[0])
    resulta.pop(-1)
    resultb.pop(-1)
    Both.pop(0)
    none.pop(0)


q = len(resultb) + len(resulta) + len(corr)
q = m - q
if elev:
    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])
    print('xx, yy = ' + str(xx) + ', ' + str(yy))
    print('xxx, yyy = ' + str(xxx)+', ' + str(yyy))
    print('resultb = ' + str(len(resultb)))
    print('resulta = ' + str(len(resulta)))
    print('Bothf = ' + str(len(Both)))
    print('Bobf = ' + str(len(Bob)))
    print('Alicf = ' + str(len(Alice)))
    print('corr = ' + str(len(corr)))
    print(Alice[0])
    print(Bob[0])
    print(Both[0])
    print(none[0])
    print(none[q+1])

All = Both + Alice + Bob + none
All.sort(key=lambda x: x[0])
if elev:
    print('q = ' + str(q))
    print('All = ' + str(len(All)))

result = All[:q]

result = resulta + resultb + result + tresult + corr

result.sort(key=lambda x: x[0])
print(sum(row[0] for row in result))
print(' '.join([str(row[3]) for row in result]))