while (line = gets) do
  person_count, initial_stone_count = line.split.map(&:to_i)
  break if  person_count == 0 && initial_stone_count == 0
  my_stones = Array.new(person_count, 0)

  stone_count = initial_stone_count
  turn_count = 0

  while true do
    index = turn_count % person_count

    if stone_count == 1
      if my_stones[index] == initial_stone_count - 1
        p index
        break
      end
    end

    if stone_count == 0
      stone_count += my_stones[index]
      my_stones[index] = 0
    else
      stone_count -= 1
      my_stones[index] +=1
    end

    turn_count += 1
  end
end