#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; template struct BinaryIndexedTree { int n; vector t; BinaryIndexedTree() {} BinaryIndexedTree(int _n) :n(_n + 1), t(_n + 1) {} void add(int k, Val val) { k++; while (k < n) { t[k] += val; k += (k&-k); } } void set(int k, Val val) { add(k, -sum(k, k + 1)); add(k, val); } Val sum(int k) { Val r = 0; while (k > 0) { r += t[k]; k -= (k&-k); } return r; } Val sum(int l, int r) { return sum(r) - sum(l); } }; const int R = 100001; BinaryIndexedTree sum[5]; vi aid[R]; int f(int b, int mi) { int res = 0; rep(i, 5) { if (i >= mi)res += sum[i].sum(0, R); else if (i + 1 >= mi)res += sum[i].sum(b, R); } return res; } int main(){ ios::sync_with_stdio(false); cin.tie(0); rep(i, 5)sum[i] = BinaryIndexedTree(R); int N, M; cin >> N >> M; vi X(N), A(N), B(N); rep(i, N) { cin >> X[i] >> A[i] >> B[i]; aid[A[i]].push_back(i); sum[X[i]].add(B[i], 1); } int ans = INT_MAX; for (int a = R - 1; a >= 0; --a) { each(i, aid[a]) { sum[X[i]].add(B[i], -1); sum[X[i] + 1].add(B[i], 1); } if (f(0, 2) < M)continue; int ok = 0, ng = R, mid; while (ng - ok > 1) { mid = (ok + ng) / 2; (f(mid, 2) >= M ? ok : ng) = mid; } smin(ans, f(ok, 3)); } cout << ans << endl; }