import Control.Monad
import Control.Applicative

main = do
        [n,m] <- map read.words<$>getLine
        unless (n == 0 && m == 0) $ do
            print $ josephLoop m [1..n]
            main

joseph :: Int -> [Int] -> [Int]
joseph _ [] = []
joseph m xs = let i = m `mod` length xs
                  (l,r) = splitAt i xs
               in if null l then init r
                            else r ++ init l

josephLoop m xs = if length xs == 1 then head xs
                                    else josephLoop m (joseph m xs)

