f m n | m < n = 1 + (f n m) | m `mod` n == 0 = (m `div` n) - 1 | otherwise = (m `div` n) + (f (m `mod` n) n) main = do [m,n] <- (map read . words) `fmap` getLine print $ f m n