#include<bits/stdc++.h>
#include<stack>

using namespace std;

int main()
{
    
    int n,m;
    cin >> n >> m;
    queue<long long> lane[11];

    for(int i=0;i<m;i++)
    {

        int count=1;
        for(int j=1;j<=n;j++)
        {
            if(lane[j].size()<lane[count].size())
            {
                count=j;   
            }
        }


        int op;
        cin >> op;
        if(op==1)
        {
            int num;
            cin >> num;
            lane[count].push(num);
        }
        else if(op==0)
        {
            int l;
            cin >> l;
            cout << lane[l].front() << endl;
            lane[l].pop();
        }
    }
    return(0);
}
