#include using namespace std; using ll = long long; #define fi first #define se second #define pb push_back #define all(v) (v).begin(),(v).end() ll mod = 1000000007; ll INF = 1000000099; //cin.tie(0); //ios::sync_with_stdio(false); signed main(){ ll n,m,nk=0,sk=0,ans=INF; cin>>n>>m; ll cnt[n];//index i番目の完答数 vector> f(n),s(n); for(int i=0;i < n;i++) { cin>>cnt[i]; cnt[i]++; cin>>f.at(i).fi>>s.at(i).fi; f.at(i).se=i; s.at(i).se=i; if(cnt[i]>=3){ sk++; nk++; }else if(cnt[i]>=2){ nk++; } } sort(f.begin(),f.end()); sort(s.begin(),s.end()); ll l=0,r=n; for(int l=0;l < n;l++) { if(l>0){ cnt[f.at(l-1).se]--; if(cnt[f.at(l-1).se]==2){ sk--; }else if(cnt[f.at(l-1).se]==1){ nk--; } if(f.at(l-1).fi==f.at(l).fi){ continue; } } while(m>nk || (r-1) r--; else break; if(rsk)cout << l<