import sequtils,strutils,algorithm,math type item = tuple[x, a, b, id : int] var N, M : int x, a, b : int A = newSeq[item](0) flag : array[100001,int] cnt : array[6, int] (N, M) = stdin.readline.split.map(parseInt) for n in 0.. -1 or i < 100001: if sum(cnt[2..5]) < M and j > -1: j -= 1 while pbj >= 0 and B[pbj].b >= j: var f = B[pbj].id cnt[flag[f]] -= 1 cnt[flag[f] + 1] += 1 flag[f] += 1 pbj -= 1 elif sum(cnt[2..5]) >= M and i < 100001: ans = min(ans, sum(cnt[3..5])) i += 1 while pai < A.len and A[pai].a <= i: var f = A[pai].id cnt[flag[f]] -= 1 cnt[flag[f] - 1] += 1 flag[f] -= 1 pai += 1 else: break echo ans