#include<iostream>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
int m,n;
bool comp(int a,int b)
{
    return !(a<b);
}
priority_queue <int> a[200002];
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        int st;
        scanf("%d",&st);
        if(st==1)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            y=-y;
            a[x].push(y);
        }
        else
        if(st==2)
        {
            int sum=-1;
            long long fc=-1;
            for(int j=1;j<=m;j++)
                {
                    //cout<<a[j].top()<<endl;
                    if(fc==-1)
                    {
                        sum=abs(a[j].top());
                        fc=j;
                    }
                    else
                    {
                        if(sum<abs(a[j].top()))
                        {sum=abs(a[j].top());
                            fc=j;}
                    }
                }
            cout<<sum<<endl;
            a[fc].pop();
        }
    }
}
