import Control.Applicative


main = do
  [n,x] <- map read . words <$> getLine :: IO [Int]

  print $ n + f x (n-x)

  where
    f a b
      | a == 0 = 0
      | a > b = f b a
      | a <= b =
        if b `mod` a == 0 then
          2*a*(b`div`a)-1
        else
          2*a*(b`div`a) + f (b`mod`a) a
