#include #define lowbit(x) x & (-x) #define ls(k) k << 1 #define rs(k) k << 1 | 1 #define fi first #define se second #define ctz(x) __builtin_ctz(x) #define popcnt(x) __builtin_popcount(x) #define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout); using namespace std; typedef __int128 __; typedef long double lb; typedef double db; typedef unsigned long long ull; typedef long long ll; bool Begin; const int N = 1e5 + 10; inline ll read(){ ll x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){ if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9'){ x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void write(ll x){ if(x < 0){ putchar('-'); x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } int n, m, now, ans, sum = 1e9; vector> V[N]; class BIT{ public: int all = 0; int a[N]; inline void add(int x, int v){ all += v; ++x; for(int i = x; i < N; i += lowbit(i)) a[i] += v; } inline int ask(int x){ int sum = 0; for(int i = x; i; i -= lowbit(i)) sum += a[i]; return all - sum; } }T1, T2; bool End; int main(){ n = read(), m = read(); for(int x, a, b, i = 1; i <= n; ++i){ x = read(), a = read(), b = read(); if(x >= 1) ++now; if(x >= 2) ++ans; if(x == 1) T1.add(b, 1); if(!x) T2.add(b, 1); V[a].push_back({x, b}); } if(now >= m) sum = min(sum, ans); for(int i = 0; i <= 1e5; ++i){ for(auto v : V[i]){ int x = v.fi, b = v.se; if(x >= 3) continue; if(x == 2){ --ans; T1.add(b, 1); } if(x == 1){ --now; T2.add(b, 1); T1.add(b, -1); } if(!x) T2.add(b, -1); } if(now >= m) sum = min(sum, ans); int l = 0, r = 1e5, sb = -1; while(l <= r){ int mid = (l + r) >> 1; if(T2.ask(mid) + now >= m){ sb = mid; l = mid + 1; } else r = mid - 1; } if(sb != -1) sum = min(sum, ans + T1.ask(sb)); } write(sum); cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB"; return 0; }