#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
// ZA koito ne nz znae VednYj

long n,m,i,j,p;
long node,weight;

priority_queue<long> a[5000];
void gather()
{int max1=0,maxind;
for(int i=1;i<=m;i++)
{
    if(-a[i].top()>max1){max1=-a[i].top();maxind=i;}
}
a[maxind].pop();
cout<<max1<<endl;

return ;
}

int main()
{cin>>n>>m;
for(i=0;i<n;i++)
{cin>>p;
if(p==2)gather();
else cin>>node>>weight;
a[node].push(-weight);

}

return 0;
}
