#include <iostream>
#include <set>
#include <list>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 200005;
const int INF = 1 << 30;
int n;

list<int> theList;
set<int> cities[MAXN];

bool cmp(int a, int b) {
    //cout << "inv";
    return (*cities[a].begin()) > (*cities[b].begin());
}

void sortTheList() {
    theList.sort(cmp);
    //sort(theList.begin(), theList.end(), cmp2);
}

void printCities() {
    cout << "ORIGINAL\n";
    for(int i = 0; i < n; i++) {
        for(set<int>::iterator it = cities[i].begin(); it != cities[i].end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "DA LIST: ";
     for(list<int>::iterator it1 = theList.begin(); it1 != theList.end(); it1++) {
        cout << *it1 << " ";
     }
     cout << endl;

    cout << "SORTED:\n";
    for(list<int>::iterator it1 = theList.begin(); it1 != theList.end(); it1++) {
        for(set<int>::iterator it = cities[*it1].begin(); it != cities[*it1].end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
    }
}

void add(int t, int w) {
    cities[t].insert(w);
    sortTheList();
}

int collect() {
    //printCities();
    int maxAt = *theList.begin();
    int max = *cities[maxAt].begin();
    cities[maxAt].erase(cities[maxAt].begin());
    sortTheList();
    return max;
    /*
    int max=-INF, maxAt, curr;
    for(int i = 0; i < n; i++) {
        curr = (*cities[i].begin());
        if(curr > max) {
            max = curr;
            maxAt = i;
        }
    }
    cities[maxAt].erase(cities[maxAt].begin());
    return max;
    */
}

void fillTheList() {
    for(int i = 0; i< n; i++) {
        theList.push_back(i);
    }
}

int main() {
    int m;
    scanf("%d %d", &m, &n);
    fillTheList();

    int op, t, w;
    int bill;
    for(int i = 0; i < m; i++) {
        scanf("%d", &op);
        if(op == 1) {
            scanf("%d %d", &t, &w);
            add(t-1, w);
        } else {
            bill = collect();
            printf("%d\n", bill);
        }
    }

    return 0;
}
