#include<iostream>
using namespace std;

struct heap { int h[200005],sz; }; //sz sochi purviq svoboden indeks za pisane

void push(heap &city,int w)
{
   int pos,swp;

   if(city.sz==1) city.h[city.sz++]=w;

   else
   {
      pos=city.sz++;
      city.h[pos]=w;

      while(pos!=1 && city.h[pos]<city.h[pos/2])
      {
         swp=city.h[pos];
         city.h[pos]=city.h[pos/2];
         city.h[pos/2]=swp;
         pos/=2;
      }
   }
}

void pop(heap &city)
{
   int pos=1,swp;

   city.h[1]=city.h[--city.sz];

   while(1)
   {
      if(pos*2>=city.sz) break;

      if(pos*2+1==city.sz && city.h[pos]>city.h[pos*2])
      {
         swp=city.h[pos];
         city.h[pos]=city.h[pos*2];
         city.h[pos*2]=swp;
         break;
      }

      if(city.h[pos*2]>city.h[pos*2+1])
      {
         swp=city.h[pos*2+1];
         city.h[pos*2+1]=city.h[pos];
         city.h[pos]=swp;
         pos=pos*2+1;
      }

      else
      {
         swp=city.h[pos*2];
         city.h[pos*2]=city.h[pos];
         city.h[pos]=swp;
         pos*=2;
      }
   }
}

int n,m;
heap city[50];

void input()
{
   int i,j,type,t,w;
   int maxw,chosen;

   cin>>n>>m;

   for(i=1;i<=m;i++)
      city[i].sz=1;

   for(i=0;i<n;i++)
   {
      cin>>type;

      if(type==1)
      {
         cin>>t>>w;
         push(city[t],w);
      }


      else
      {
         /*for(i=1;i<=m;i++)
         {
            cout<<"      grad"<<i<<"  ";
            for(j=1;j<city[i].sz;j++)
               cout<<city[i].h[j]<<' ';
            cout<<endl;
         }*/

         maxw=0;
         for(j=1;j<=m;j++)
            if(city[j].h[1]>maxw)
            {
               maxw=city[j].h[1];
               chosen=j;
            }
         cout<</*"      out: "<<*/city[chosen].h[1]<<endl;
         pop(city[chosen]);
      }
   }
}

int main()
{
   input();

   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

=>
4
3
5
*/
