import Control.Applicative

solve a b t
  | a == b = t + a
  | a < b = solve a (b-a) (t+2*a)
  | otherwise = solve (a-b) b (t+2*b)

main = do
  [n,x] <- map read.words <$> getLine :: IO [Integer]
  print $ solve x (n-x) n
