import Control.Monad --import Debug.Trace saidaikouyakusuu a b r | r == 0 = b | otherwise = saidaikouyakusuu b r (b `mod` r) pyon :: (Integral a) => a -> a -> a pyon n k | (n - k == 0) || (k == 1) = n - 1 | r == 1 = n - 1 | otherwise = n' - 1 where r = saidaikouyakusuu n k (n `mod` k) n' = div n r main = do nk <- map (read :: String -> Int) . words <$> getLine let hasira = [0] ++ take (n - 1) [1,1..] n = head nk k = last nk print $ pyon n k