#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct cmp{
    bool operator()(const int a,const int b){
        return a>b;
    }
};
int n,m,o,d,c;
int maxy,maxi;
priority_queue<int,vector<int>,cmp> city[5100];
int main(){
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;++i){
        scanf("%d",&o);
        if (o==1){
            scanf("%d%d",&c,&d);
            city[c-1].push(d);
        }
        else {
            maxy=0;
            for (int j=0;j<m;++j){
                if (city[j].top()>maxy){maxy=city[j].top();maxi=j;}
            }
            printf("%d\n",maxy);city[maxi].pop();
        }
    }
    return 0;
}
