while true
  n, p = gets.split(" ").map(&:to_i)
  s = p
  break if n == 0 && p == 0

  i = 0
  h = Hash.new(0)
  while true
    if p > 0
      p -= 1
      h[i] += 1
      if h[i] == s
        puts i
        break
      end
    else
      if h[i] > 0
        p = h[i]
        h[i] = 0
      end
    end
    i = (i == n-1) ? 0 : i+1
  end
end