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 == 1 = acc + y | x < y = dp (acc+1) y x | otherwise = dp (acc+1) (x-y) y