main = getLine >>= print . koba . map read . words koba [m,n] = let d = gcd m n in dp 0 (div m d) (div n d) dp acc x y | x == y = acc | x == 1 = acc + y | y == 1 = acc + x - 1 | x > y = let (q,r) = divMod x y in dp (acc+q) r y | otherwise = dp (acc+1) y x