n,k = map(int,input().split())
l = []
for _ in range(n):
    l.append(list(map(int,input().split())))
a,b = 0,0
s = 0
l.sort(key = lambda x:[-x[1],-x[2],x[0]])
for i in range(n):
    s += l[i][0]
    a += l[i][1]
    b += l[i][2]
    if a >= k and b >= k:
        break
if a >= k and b >= k:
    print(s)
else:
    print(-1)