#include /* #include #include #include #include */ using namespace std; //type typedef long long ll; #define pii pair #define vi vector #define vl vector //x * y * 1.0 can cause overflow //constant #define inf (int)(1e9+7) #define mod (ll)(1e9+7) #define eps 1e-10 int dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; int dx[] = {1, -1, 0, 0, -1, 1, 1, -1}; //omission #define eb emplace_back #define F first #define S second #define SZ(x) ((int)(x).size()) #define all(v) v.begin(), v.end() //manip template bool chmax(T &a, T b) {if (a < b) {a = b; return 1;} return 0;} template bool chmin(T &a, T b) {if (a > b) {a = b; return 1;} return 0;} #define UNIQUE(v) v.erase(unique(v.begin(), v.end(), v.end()) #define fill(x, y) memset(x, y, sizeof(x)) #define ceil(a, b) a / b + !!(a % b) template T power(T a, T b) {return b ? power(a * a % inf, b / 2) * (b % 2 ? a : 1) % inf : 1;} #define LB(v, x) (int)(lower_bound(v.begin(), v.end(), x) - v.begin()) #define UB(v, x) (int)(upper_bound(v.begin(), v.end(), x) - v.begin()) //output #define out(a) cout << a << endl #define outa(a, n) rep(i, n) cout << a[i] << " "; cout << endl #define outv(v) rep(i, SZ(v)) cout << v[i] << " "; cout << endl; #define outp(v) rep(i, SZ(v)) cout << v[i].F << " " << v[i].S << endl #define outc(s, t) cout << fixed << setprecision(10) << (double)(t - s) / CLOCKS_PER_SEC << endl //loop #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define rep3(i, st, n) for (int i = st; i < n; ++i) #define rrep(i, n) for (int i = n; i >= 0; --i) //algorithm (have library) //double pointer, l start, how many adds, can be 0 -> init r = l, sum = 0, while r < n bool comp(pair a, pair b) { //if (a.S.F != b.S.F) return a.S.F > b.S.F; //return a.S.S > b.S.S; return a.S.F > b.S.F; } bool comp2(pair a, pair b) { //if (a.S.F != b.S.F) return a.S.F > b.S.F; //return a.S.S > b.S.S; return a.S.S < b.S.S; } int main() { //cast caution //look constraints always cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; int c = 0; vector > v; int acm3 = 0, acm2 = 0; //ac more than or equal to int x[n]; fill_n(x, n, 0); rep(i, n) { int a, b; cin >> x[i] >> a >> b; x[i]++; //a全員解けてる状態 if (x[i] - 1 != 3) v.eb(i, pii(a, b)); else { //n--; c++; } if (x[i] >= 3) acm3++; if (x[i] >= 2) acm2++; } //n -= c; if (c >= m) { //入力の時点で3以上の //cout << "c "; cout << c << endl; return 0; } sort(all(v), comp); vector > v2 = v; sort(all(v2), comp2); int ans = inf; if (acm2 >= m) ans = acm3; rep(sa, 100002) { int flg = 0; while (SZ(v) && v[v.size() - 1].S.F == sa - 1) { //手前の人が解けない x[v[v.size() - 1].F]--; if (x[v[v.size() - 1].F] == 2) acm3--; else if (x[v[v.size() - 1].F] == 1) acm2--; v.erase(v.end() - 1); } if (acm2 >= m) { chmin(ans, acm3); if (SZ(v)) continue; else flg++; } for (int sb = v2[SZ(v2) - 1].S.S; sb >= 0; --sb) { if (SZ(v2) <= 0) {flg++; break;} while (v2[SZ(v2) - 1].S.S == sb) { x[v2[SZ(v2) - 1].F]++; if (x[v2[SZ(v2) - 1].F] == 2) acm2++; if (x[v2[SZ(v2) - 1].F] == 3) acm3++; v2.erase(v2.end() - 1); if (SZ(v2) <= 0) {flg++; break;} } if (flg) break; if (acm2 >= m) break; } if (flg == 2) break; if (acm2 >= m) { chmin(ans, acm3); } if (!SZ(v)) break; } //cout << "ans "; cout << ans << endl; } //cast caution /* 目的は2つ 1. 2完以上をm人以上 2. 3完以上を最小化 1. の上で2. を目指す 「2完以上m人付近」を全探索すれば答が求まる 毎回操作が一意になるようにしたい 最初 a 0, b infにする 2完以上m人以上ならa厳しく m人未満ならbやさしく と、一意に決まる a 0, b inf 最初m人付近ではないこともある m人付近まで なんかたどる あとはm人付近をうろうろ a inf, b 0でも 逆の経路みたいになる m人付近では同じ */