#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
priority_queue <int> q[200001];
pair <int, int> tree[1<<19];
int n,m,type,t,x,y,p=1;
/*int LB (int x)
{
    return x&(-x);
}
void update1 (int x, int y)
{
    for(int i=x;i<=n;i+=LB(i))
        if (tree[i]!=0) tree[i]=min(tree[i],y);
}
void update2 (int x)
{
    for(int i=x;i<=n;i+=LB(i))
        tree[i]=0;
}
int query ()
{
    for(int i=n;i>=1;i-=LB(i))
        res=max(res,tree[i]);
    return res;
}*/
void update (int x, int y)
{
    x=x+p-1;
    tree[x].first=y;
    tree[x].second=x-p+1;
    while(x>1)
    {
        x=x>>1;
        if (tree[x<<1].first>tree[(x<<1)+1].first) {tree[x]=tree[x<<1];}
        else {tree[x]=tree[(x<<1)+1];}
    }
}
int main()
{
    scanf("%d%d",&m,&n);
    while(p<n)
       p=p<<1;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&type);
        if (type==2)
        {
            for(int j=1;j<=n;j++)
            {
                t=-q[j].top();
                if (tree[j+p-1].first!=t) update(j,t);
            }
            printf("%d\n",tree[1].first);
            q[tree[1].second].pop();
            continue;
        }
        scanf("%d%d",&x,&y);
        q[x].push(-y);
    }
	return 0;
}
/*
9 2
1 1 9
1 2 3
1 1 4
2
1 1 2
1 2 5
2
2
1 2 1
*/
