#include #include #include #include using namespace std; template struct FenwickTree { std::vector dat; FenwickTree(int n) : dat(n + 1) {} void add(int k, T v) { for (k++; k > 0; k &= k - 1) { dat[k] += v; } } T sum(int k) { T ret = 0; for (k++; k < dat.size(); 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); FenwickTree ft1(1e5 + 10); FenwickTree ft2(1e5 + 10); 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] == 1) { ft1.add(b[i], 1); } if (x[i] == 2) { two++; ft2.add(b[i], 1); } if (x[i] == 3) { two++; three++; } } 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 = 0; int ng = 1e5 + 5; while (ng - ok > 1) { int mid = (ok + ng) / 2; if (ft1.sum(mid) + two >= m) { ok = mid; } else { ng = mid; } } ans = min(ans, ft2.sum(ok) + three); if (i == n) break; int j = i; while (i < n && a[p[i]] == a[p[j]]) { int ii = p[i]; x[ii]++; if (x[ii] == 3) { ft2.add(b[ii], -1); three++; } else if (x[ii] == 2) { ft2.add(b[ii], 1); ft1.add(b[ii], -1); two++; } else if (x[ii] == 1) { ft1.add(b[ii], 1); } i++; } } cout << ans << endl; }