import Data.IntMap.Lazy (adjust, fromList, (!))

main = interact $ unlines . map (show . next_mayor . map read . words) . takeWhile (/= "0 0") . lines

next_mayor [n,p] = game p 0 (fromList (zip [0..n-1] (repeat 0)))
 where
  game p' n' im
   | p'==1 && im!n'==(p-1) = n'
   | p'==0 = game (im!n') (mod (n'+1) n) (adjust (const 0) n' im)
   | otherwise = game (p'-1) (mod (n'+1) n) (adjust succ n' im)