import sys readline=sys.stdin.readline N,M=map(int,readline().split()) X,A,B=[],[],[] idx=[[] for i in range(10**5+1)] for i in range(N): x,a,b=map(int,readline().split()) X.append(x) A.append(a) B.append(b) if a==b: idx[a].append(i) else: idx[a].append(i) idx[b].append(i) cnt=[0]*6 for x in X: cnt[x+1]+=1 b=10**5+1 ans=N for a in range(10**5+2): if a: for i in idx[a-1]: x=X[i] if A[i]>=a-1: x+=1 if B[i]>=b: x+=1 cnt[x]-=1 x=X[i] if A[i]>=a: x+=1 if B[i]>=b: x+=1 cnt[x]+=1 while b and sum(cnt[2:])=a: x+=1 if B[i]>=b: x+=1 cnt[x]-=1 x=X[i] if A[i]>=a: x+=1 if B[i]>=b-1: x+=1 cnt[x]+=1 b-=1 if sum(cnt[2:])>=M: ans=min(ans,sum(cnt[3:])) print(ans)