L = 100001 n,m = map(int,input().split()) people = [tuple(map(int,input().split())) for _ in range(n)] by_a = [[] for _ in range(L)] by_b = [[] for _ in range(L)] for x,a,b in people: by_a[a].append((x, b)) by_b[b].append((x, a)) def it(): c0 = sum(int(x >= 1) for x, a, b in people) c1 = sum(int(x >= 2) for x, a, b in people) sa = 0 sb = L while sa <= L: if sa > 0: for x, b in by_a[sa-1]: x += int(b>=sb) if x == 1: c0 -= 1 elif x == 2: c1 -= 1 while c0 < m and 0 < sb: sb -= 1 for x, a in by_b[sb]: x += int(a>=sa) if x == 1: c0 += 1 elif x == 2: c1 += 1 if sb < 0: return yield c1 sa += 1 print(min(it()))