#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
priority_queue <int> q[200001];
priority_queue < pair <int, int> > p;
int n,m,type,t,x,y;
int main()
{
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&type);
        if (type==2)
        {
            for(int j=1;j<=n;j++)
            {
                t=-q[j].top();
                p.push(make_pair(t,j));
            }
            printf("%d\n",p.top().first);
            q[p.top().second].pop();
            p.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
*/
