#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if (y < x) x = y; } template static void amax(T &x, U y) { if (x < y) x = y; } int main() { int N; int M; while (~scanf("%d%d", &N, &M)) { const int S = 100002; vector eventA(S), eventB(S); vector Xs(N, 0); array cnt = { {} }; rep(i, N) { int X; int A; int B; scanf("%d%d%d", &X, &A, &B); ++ X; Xs[i] = X; eventA[A + 1].push_back(i); eventB[B].push_back(i); ++ cnt[X]; } int b = S - 1; int ans = INF; for (int a = 0; a < S; ++ a) { for (int i : eventA[a]) { int x = Xs[i] --; -- cnt[x], ++ cnt[x - 1]; } for (; b >= 0; -- b) { if (cnt[2] + cnt[3] + cnt[4] + cnt[5] >= M) break; for (int i : eventB[b]) { int x = Xs[i] ++; -- cnt[x], ++ cnt[x + 1]; } } if (cnt[2] + cnt[3] + cnt[4] + cnt[5] >= M) { amin(ans, cnt[3] + cnt[4] + cnt[5]); } } printf("%d\n", ans); } return 0; }