#!/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 = [x + 1 for x in 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) counts = [0, 0, 0, 0, 0, 0] for x in X: counts[x] += 1 A = 0 B = 10 ** 5 + 1 answer = 10 ** 9 while True: if A >= U: break if sum(counts[2:]) >= M and sum(counts[3:]) < answer: answer = sum(counts[3:]) 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 print(answer)