dfs a b
  | a == 0    = - b
  | otherwise = 2 * a * (b `div` a) + dfs (b `mod` a) a


solve (n : x : _) = n + dfs a b
  where
    a = min x (n - x)
    b = max x (n - x)

main = getLine >>= print . solve . map read . words
