import Control.Applicative
main = do
  v <- takeWhile (\x-> x/= [0,0]) . map (map (read::String->Int) . words) . lines <$> getContents
  mapM_ (putStrLn . show . solve) v

solve::[Int] -> Int 
solve (n:m:_) = joseph m 1 [1..n]

joseph:: Int -> Int -> [Int] -> Int
joseph m p xs
  | length xs==1 = head xs
  | otherwise = joseph m pNext xsNext
  where
     hit = last . take m . dropWhile (/= p)  $ cycle xs
     xsNext = filter (/= hit) xs
     pNext = head . tail . dropWhile (/= hit)  $ cycle xs