#include using namespace std; //#pragma GCC optimize("Ofast") #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<> #define rev(x) reverse(x); using ll=long long; using vl=vector; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[9]={1,0,-1,0,1,1,-1,-1,0}; const ll dx[9]={0,1,0,-1,1,-1,1,-1,0}; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(){ ll n,m;cin >> n >> m; vl x(n),a(n),b(n); rep(i,n)cin >> x[i] >> a[i] >> b[i]; ll base=100001; vvl ia(base+1),ib(base+1); vl tp(6); rep(i,n){ ia[a[i]].emplace_back(i); ib[b[i]].emplace_back(i); tp[x[i]+1]++; } ll right=base+1; ll ans=INF; for(ll left=0;left<=base;left++){ while(right>=1){ if(tp[2]+tp[3]+tp[4]+tp[5]>=m)break; for(auto p:ib[right-1]){ ll f=x[p]+(a[p]>=left? 1:0); tp[f]--;tp[f+1]++; } right--; } //if(left<=20)cout << left <<" " << right << endl; if(tp[2]+tp[3]+tp[4]+tp[5]>=m)chmin(ans,tp[3]+tp[4]+tp[5]); for(auto p:ia[left]){ ll f=x[p]+1+(b[p]>=right? 1:0); tp[f]--;tp[f-1]++; } } cout << ans << endl; }