loop do
  n, p = gets.split.map(&:to_i)
  total = p
  break if n == 0 && p == 0
  stone = [0] * n
  (0..1/0.0).each do |i|
    x = i % n
    if p == 0
      if stone[m = (x + n - 1) % n] == total
        puts m
        break
      else
        if stone[x] > 0
          p = stone[x]
          stone[x] = 0
        end
      end
    else
      p -= 1
      stone[x] += 1
    end
  end
end