#include #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i,n) for(int i=0;i<(int)n;++i) #define each(a,b) for(auto (a): (b)) #define all(v) (v).begin(),(v).end() #define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end()) #define fi first #define se second #define pb push_back #define show(x) cout<<#x<<" = "<<(x)<P; const int MAX_N = 100005; vector sa[MAX_N],sb[MAX_N]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n,m; cin >> n >> m; vector x(n),a(n),b(n); rep(i,n){ cin >> x[i] >> a[i] >> b[i]; x[i]++; sa[a[i]].pb(i),sb[b[i]].pb(i); } int na = 0,nb = 100001; int two=0,three=0; rep(i,n){ if(x[i] >= 2){ two++; if(x[i] >= 3){ three++; } } } int ans = INF; while(1){ if(two >= m){ ans = min(ans,three); na++; if(na > 100001){ break; } rep(i,sa[na-1].size()){ x[sa[na-1][i]]--; if(x[sa[na-1][i]] == 1){ two--; }else if(x[sa[na-1][i]] == 2){ three--; } } }else{ nb--; if(nb < 0){ break; } rep(i,sb[nb].size()){ x[sb[nb][i]]++; if(x[sb[nb][i]] == 2){ two++; }else if(x[sb[nb][i]] == 3){ three++; } } } } cout << ans << endl; return 0; }