main = mapM_ (print . solve . map read . words) . lines =<< getContents
solve [n,x] = if x*2>n then solve [n,n-x] else f x (n-x)
f a b
  |a==0 = 0
  |otherwise = 3*a*d + f r a
  where (d,r) = b`divMod`a