#include #include #include #include using namespace std; constexpr int N = 1e5 + 10; int dat[N]; void add(int k, int v) { for (k++; k < N; k += k & -k) { dat[k] += v; } } int sum(int k) { int ret = 0; for (k++; k > 0; k -= k & -k) { ret += dat[k]; } return ret; } int main() { int n, m; cin >> n >> m; vector p(n); vector x(n), a(n), b(n); int two = 0; int three = 0; for (int i = 0; i < n; i++) { p[i] = i; scanf("%d %d %d", &x[i], &a[i], &b[i]); if (x[i] >= 2) two++; if (x[i] >= 3) three++; if (x[i] == 2) { add(b[i], 1); } } sort(p.begin(), p.end(), [&](int i, int j) { return a[i] < a[j]; }); int ans = 1e9; int i = 0; while (i <= n) { int ok = 1e5; int ng = -1; while (ok - ng > 1) { int mid = (ok + ng) / 2; if (sum(mid) + two >= m) { ok = mid; } else { ng = mid; } } ans = min(ans, sum(ok) + three); if (i == n) break; int j = i; while (i < n && a[p[i]] == a[p[j]]) { x[p[i]]++; if (x[p[i]] == 3) { add(b[p[i]], 1); } else if (x[p[i]] == 2) { add(b[p[i]], -1); } i++; } } cout << ans << endl; }