n,m,ke=[int(i) for i in input().split()]
lis,bo,a1,b1,no=[],[],[],[],[]
for i in range(n):
    ti,a,b=[int(i) for i in input().split()]
    if a==1 and b==1:
        bo.append([ti,i+1])
    elif a==1:
        a1.append([ti,i+1])
    elif b==1:
        b1.append([ti,i+1])
    else:
        no.append([ti,i+1])
bo.sort(),a1.sort(),b1.sort(),no.sort()
su,i,j,k=0,0,0,0
while ke>0:
    if i>=len(bo) or (j>=len(a1) or k>=len(b1)):
        break
    elif bo[i][0]<=a1[j][0]+b1[k][0]:
        su+=bo[i][0]
        lis.append(bo[i][1])
        i,ke,m=i+1,ke-1,m-1
    elif bo[i][0]>a1[j][0]+b1[k][0]:
        if m-2<ke-1:
            su += bo[i][0]
            lis.append(bo[i][1])
            i, ke, m = i + 1, ke - 1, m - 1
        else:
            su+=a1[j][0]+b1[k][0]
            lis.append(a1[j][1]),lis.append(b1[k][1])
            ke,m,j,k=ke-1,m-2,j+1,k+1
if ke>0 and i>=len(bo) and (j>=len(a1) and k>=len(b1)):
    print(-1)
elif ke==0 and m==0:
    print(su)
    print(*lis)
else:
    if ke==0:
        i1 = 0
        f1 = 0
        while m > 0:
            if i1 >= len(no) and i >= len(bo) and j >= len(a1) and k >= len(b1):
                f1 = 1
                break
            else:
                ld = []
                if i1 < len(no):
                    ld.append([*no[i1], 0])
                if i < len(bo):
                    ld.append([*bo[i], 1])
                if j < len(a1):
                    ld.append([*a1[j], 2])
                if k < len(b1):
                    ld.append([*b1[k], 3])
                mi = min(ld)
                su += mi[0]
                lis.append(mi[1])
                m -= 1
                if mi[2] == 0:
                    i1 += 1
                elif mi[2] == 1:
                    i += 1
                elif mi[2] == 2:
                    j += 1
                elif mi[2] == 3:
                    k += 1
        if f1 == 1:
            print(-1)
        else:
            print(su)
            print(*lis)
    elif i>=len(bo):
        if (j+ke<=len(a1) and k+ke<=len(b1)):
            if ke*2>m:
                print(-1)
            else:
                while ke>0:
                    su+=a1[j][0]+b1[k][0]
                    lis.append(a1[j][1]),lis.append(b1[k][1])
                    ke,m,j,k=ke-1,m-2,j+1,k+1
                if m==0:
                    print(su)
                    print(*lis)
                else:
                    i1=0
                    f1=0
                    while m>0:
                        if i1>=len(no) and i>=len(bo) and j>=len(a1) and k>=len(b1):
                            f1=1
                            break
                        else:
                            ld=[]
                            if i1<len(no):
                                ld.append([*no[i1],0])
                            if i<len(bo):
                                ld.append([*bo[i],1])
                            if j<len(a1):
                                ld.append([*a1[j],2])
                            if k<len(b1):
                                ld.append([*b1[k],3])
                            mi=min(ld)
                            su+=mi[0]
                            lis.append(mi[1])
                            m-=1
                            if mi[2]==0:
                                i1+=1
                            elif mi[2]==1:
                                i+=1
                            elif mi[2]==2:
                                j+=1
                            elif mi[2]==3:
                                k+=1
                    if f1==1:
                        print(-1)
                    else:
                        print(su)
                        print(*lis)
        else:
            print(-1)
    elif j+1>len(a1) or k+1>len(b1):
        if i+ke<=len(bo):
            while ke>0:
                su+=bo[i][0]
                lis.append(bo[i][1])
                i,ke,m=i+1,ke-1,m-1
            if m==0:
                print(su)
                print(*lis)
            else:
                i1 = 0
                f1 = 0
                while m > 0:
                    if i1 >= len(no) and i >= len(bo) and j >= len(a1) and k >= len(b1):
                        f1 = 1
                        break
                    else:
                        ld = []
                        if i1 < len(no):
                            ld.append([*no[i1], 0])
                        if i < len(bo):
                            ld.append([*bo[i], 1])
                        if j < len(a1):
                            ld.append([*a1[j], 2])
                        if k < len(b1):
                            ld.append([*b1[k], 3])
                        mi = min(ld)
                        su += mi[0]
                        lis.append(mi[1])
                        m -= 1
                        if mi[2] == 0:
                            i1 += 1
                        elif mi[2] == 1:
                            i += 1
                        elif mi[2] == 2:
                            j += 1
                        elif mi[2] == 3:
                            k += 1
                if f1 == 1:
                    print(-1)
                else:
                    print(su)
                    print(*lis)
        else:
            print(-1)
    else:
        print(-1)


