n, q = gets.split.map &:to_i
lane = Array.new(n) { Queue.new }
q.times do
	com, x = gets.split.map &:to_i
	if com == 0
		puts lane[x - 1].pop
	elsif com == 1
		pos = lane.each_with_index.min_by { |v, i| v.size }[1]
		lane[pos].push(x)
	end
end

