#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, M = map(int, readline().split()) m = map(int, read().split()) X, A, B = zip(*zip(m, m, m)) X = list(X) U = 10 ** 5 + 1 A_to_I = [[] for _ in range(U)] B_to_I = [[] for _ in range(U)] for i, a in enumerate(A): A_to_I[a].append(i) for i, b in enumerate(B): B_to_I[b].append(i) A = 10 ** 5 + 1 B = 10 ** 5 + 1 counts = [0, 0, 0, 0, 0, 0] for x in X: counts[x] += 1 while sum(counts[2:]) < M: A -= 1 for i in A_to_I[A]: counts[X[i]] -= 1 X[i] += 1 counts[X[i]] += 1 answer = sum(counts[3:]) while True: if A == U: break for i in A_to_I[A]: counts[X[i]] -= 1 X[i] -= 1 counts[X[i]] += 1 A += 1 while B > 0 and sum(counts[2:]) < M: B -= 1 for i in B_to_I[B]: counts[X[i]] -= 1 X[i] += 1 counts[X[i]] += 1 if sum(counts[2:]) < M: break if answer > sum(counts[3:]): answer = sum(counts[3:]) print(answer)