import Control.Applicative ((<$>), (<*>))
import Control.Monad
import Data.List
import Control.Monad.State
import Data.Array.Unboxed

main :: IO ()
main = do
  [n,p] <- (getl $ map read . words)
  unless (n == 0 && p == 0) $ do
    print $ solve n p
    main

solve :: Int -> Int -> Int
solve n p = evalState (mayer n p) (arr, 0, p)
  where
    arr = listArray (0,n-1) $ repeat 0

mayer :: Int -> Int -> State (UArray Int Int, Int, Int) Int
mayer n p = do
  (arr, i, c) <- get
  let ls = filter (\x -> snd x == p) $ assocs arr
  if null ls
    then do
      if c == 0
        then do
          put (arr // [(i,0)], (i+1) `mod` n, c + arr ! i)
          mayer n p
        else do
          put (arr // [(i, (arr ! i) + 1)], (i+1) `mod` n, c - 1)
          mayer n p
    else do
      return (fst $ head ls)

getl :: (String -> a) -> IO a
getl f = f <$> getLine