local ior = io.input()
local n, x = ior:read("*n", "*n")
local sum = n
local a, b = x, n - x
if(a < b) then a, b = b, a end
while(true) do
  if(a % b == 0) then
    sum = sum + b * (2 * (a // b) - 1)
    break
  else
    local rem = a % b
    sum = sum + 2 * (a - rem)
    a, b = b, rem
  end
end
print(sum)
