import Prelude

lightpre :: Int-> Int -> Int -> Int 
lightpre a 0 sum = sum-a
lightpre a b sum = lightpre b (a `mod` b) (sum+2 * (a `div` b)* b)

light n x = lightpre (n-x) x n

main :: IO ()
main = do
    -- スペース区切り整数の入力
    [n, x] <- map read . words <$> getLine
    -- 出力
    putStrLn $ show $ light n x

