### classes

class SegTree
  def initialize(q, lim)
    @q = 1
    while @q < q
      @q <<= 1
    end
    @lim = lim
    @data = (@q * 2 - 1).times.map{0}
    @head = @tail = 0
  end

  def add
    s = @tail + @q - 1
    while s > 0
      @data[s] += 1
      s = (s - 1) / 2
    end
    @data[0] += 1
    @lim -= 1
    ret = @tail
    @tail += 1
    del_pos(@head) if @lim < 0
    ret
  end

  def find(x)
    s = 0
    while s < @q - 1
      s0 = s * 2 + 1
      k = @data[s0]
      if x <= k
        s = s0
      else
        x -= k
        s = s0 + 1
      end
    end
    s - (@q - 1)
  end

  def del_pos(x)
    s = x + @q - 1
    while s > 0
      @data[s] -= 1
      s = (s - 1) / 2
    end
    @data[0] -= 1
    @lim += 1

    while @head < @tail && @data[@head + @q - 1] == 0
      @head += 1
    end
  end

  def del(x)
    s = 0
    while s < @q - 1
      @data[s] -= 1
      s0 = s * 2 + 1
      k = @data[s0]
      if x <= k
        s = s0
      else
        x -= k
        s = s0 + 1
      end
    end
    @data[s] -= 1
    @lim += 1

    while @head < @tail && @data[@head + @q - 1] == 0
      @head += 1
    end
  end

  def print
    p ['@head', @head, '@tail', @tail]
    l = 1
    s = 0
    while l <= @q
      p @data[s, l]
      s += l
      l *= 2
    end
  end
end

### main

loop do
  q, lim = gets.split.map(&:to_i)
  break if (q | lim) == 0

  st = SegTree.new(q, lim)
  id2seq = {}
  seq2id = []
  #p st

  q.times do
    qi, xi = gets.split.map(&:to_i)
    #p ['query', qi, xi]

    case qi
    when 0
      seq = st.add
      seq2id[seq] = xi
      id2seq[xi] = seq
    when 1
      st.del(xi)
    when 2
      seq = st.find(xi)
      puts seq2id[seq]
      #p ['q2', seq, seq2id[seq]]
    when 3
      seq = id2seq[xi]
      st.del_pos(seq)
    end

    #st.print
    #p seq2id
    #p id2seq
  end

  puts 'end'
end