/* ?????? ?????? ?????? ?????? D P ???? ?????? ?????? ?????? ?????? ??? l l? ?????? ?????? ?? OI ?? ?????? */ #include using namespace std; #define int long long //#define eps 1e-9 //#define ENF 1e13 inline int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-48;ch=getchar();} return x*f; } void write(int x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } const int N=3e5; const int mod=1e9+7; int n,m; int T[N]; struct node{ int x,a,b,id; }t[N]; int top; int ans; bool cmp(node x,node y){ return x.a>y.a; } bool ccmp(node x,node y){ return x.b>y.b; } node tt[N]; struct ns{ int two,three; }sum[N]; struct tree{ int t[N]; int lowbit(int x){ return x&(-x); } void up(int x,int k){ while(x<=100001){ t[x]+=k; x+=lowbit(x); } return; } int qry(int x){ int ret=0; while(x){ ret+=t[x]; x-=lowbit(x); } return ret; } }; tree z,zz; bool check(int x){ if(x==0)return z.qry(100001)-z.qry(0)+zz.qry(100001)-zz.qry(0)>=m; if(z.qry(100001)-z.qry(x-1)+zz.qry(100001)-zz.qry(x-1)>=m)return 1; return 0; } signed main(){ // freopen("difficulty.in","r",stdin); // freopen("difficulty.out","w",stdout); n=read(),m=read(); for(int i=1;i<=n;i++){ int d=read(),a=read()+1,b=read()+1; if(d>=3){ ans++; m--; continue; } if(d>=2)m--; top++; t[top]={d,a,b,top}; tt[top]=t[top]; T[top]=d; } sort(t+1,t+1+top,cmp); sort(tt+1,tt+1+top,ccmp); int sum0=0,sum1=0; int sm=INT_MAX; int r=0; int U=0; for(int i=1;i<=top;i++){ if(tt[i].x==1) z.up(tt[i].b,1); if(tt[i].x==2) zz.up(tt[i].b,1); } int op=0; int i=0; int L=0; int R=100001; int As=0; while(L<=R){ int mid=(L+R)>>1; if(check(mid))L=mid+1,As=mid; else R=mid-1; } // cout<=0){ if(As==0){ sm=min(sm,op+zz.qry(100001)); } else sm=min(sm,op+zz.qry(100001)-zz.qry(As-1)); } while(i<=top){ i++; if(i>top)break; while(t[i].a==t[i+1].a){ if(m<=0){ sm=min(sm,op); break; } // cout<>1; if(check(mid))l=mid+1,as=mid; else r=mid-1; } // cout<=0){ if(as==0){ sm=min(sm,op+zz.qry(100001)); continue; } sm=min(sm,op+zz.qry(100001)-zz.qry(as-1)); } } cout<