import Control.Applicative
import Data.List (sort)

solve a b t
  | a == b = t + a
  | otherwise =
    let [x,y] = sort [a,b]
        z = head [ c | c<-[y,y-1..1], x*c<y]
    in solve x (y-z*x) (t+2*z*x)

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