#
# Next Mayor
#
n, total_stones = gets.chomp.split.map { |s| s.to_i}
while [n, total_stones] != [0, 0]
  left_stones = total_stones
  my_stones = []
  n.times do
    my_stones << 0
  end

  
  winner = -1
  catch(:break) do
    loop do
      n.times do |i|
        # p "debug"
        # p left_stones
        # p my_stones

        if left_stones == 1 and  my_stones[i] == (total_stones-1)
          winner = i
          throw(:break)
        elsif left_stones == 0
          left_stones += my_stones[i]
          my_stones[i] = 0
        else
          left_stones -= 1
          my_stones[i] += 1
        end
      end
    end
  end
  puts winner
  
  n, total_stones = gets.chomp.split.map { |s| s.to_i}
end