n,m,k = map(int,input().split())
ab = []
a = []
b = []
other = []
for i in range(n):
    t,c,d = map(int,input().split())
    if c and d == 0:
        a.append([t,i+1])
    elif d and c == 0:
        b.append([t,i+1])
    elif c*d:
        ab.append([t,i+1])
    else:
        other.append([t,i+1])
a.sort()
b.sort()
ab.sort()
la = len(a)
lb = len(b)
lab = len(ab)
s = set()
dis = set()
if la+lab < k or lb+lab < k:
    print(-1)
    exit()
if lb > la:
    la,lb = lb,la
    a,b = b,a
ans = 0
if lab >= k:
    for i in range(k):
        ans += ab[i][0]
        s.add(ab[i][1])
    now = k-1
    na = 0
    nb = 0
    
else:
    for i,j in ab:
        ans += i
        s.add(j)
    for i in range(k-lab):
        ans += a[i][0]+b[i][0]
        s.add(a[i][1])
        s.add(b[i][1])
    now = lab-1
    na = k-lab
    nb = k-lab
while nb < lb and now >= 0 and ab[now][0] > a[na][0]+b[nb][0]:
    ans -= ab[now][0]-a[na][0]-b[nb][0]
    s.add(a[na][1])
    s.add(b[nb][1])
    dis.add(ab[now][1])
    na += 1
    nb += 1
    now -= 1
ab.append([float("INF"),-1])
s2 = set()
for i in s:
    if i in dis:
        continue
    s2.add(i)
s = set()
dis = set()
if len(s2) >= m:
    q = len(s2)-m
    now += 1
    for i in range(q):
        na -= 1
        nb -= 1
        ans += ab[now][0]-a[na][0]-b[na][0]
        s.add(ab[now][1])
        dis.add(a[na][1])
        dis.add(b[na][1])
        now += 1
else:
    for i in range(la):
        if a[i][1] not in s2:
            other += a[i:]
            break
    for i in range(lb):
        if b[i][1] not in s2:
            other += b[i:]
            break
    for i in range(lab):
        if ab[i][1] not in s2:
            other += ab[i:]
            break
    other.sort()
    for i in range(m-len(s2)):
        ans += other[i][0]
        s.add(other[i][1])

s |= s2
count = []
for i in s:
    if i in dis:
        continue
    count.append(i)
if ans == float("INF"):
    print(-1)
else:
    print(ans)
    print(*count)

