import Data.List

solve n x | m == 0    = len - x
          | otherwise = len + (solve x m)
    where
      (d, m) = n `divMod` x
      len = d * x * 2

run::[Integer]->Integer
run (n:x:[]) = b - a + (solve n x)
    where
      b = n - x
      a = x

f = show . run . map read . words

main = interact f