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

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

solve :: Int -> Int -> IO ()
solve n p = do
  arr <- newListArray (0,n-1) (repeat 0) :: IO (IOUArray Int Int)
  mayer n p arr 0 p
  
mayer :: Int -> Int -> IOUArray Int Int -> Int -> Int -> IO ()
mayer n p arr i c = do
  x <- readArray arr i
  if c == 0
    then do
      writeArray arr i 0
      mayer n p arr ((i+1) `mod` n) x
    else do
      if x == p-1
        then do
          print i
        else do
          writeArray arr i (x+1)
          mayer n p arr ((i+1) `mod` n) (c-1)
    
getl :: (String -> a) -> IO a
getl f = f <$> getLine