n,x = gets.split.map(&:to_i)
m = n.to_f/2
$array = []
$sum = 0

# if x < m
#   array.push(x)
#   array.push(n-x)
#   array.push(n-array[1])
#   long = array[-1]
#   sum += array[0] + array[1]
#   puts sum
#   puts
#   while true
#     if long % array[-1] == 0
#       sum += array[-1]*(long/array[-1]*2-1)
#       break
#     else
#       sum += array[-1]*(long/array[-1]*2)
#       array.push(long%array[-1])
#       long = array[-4]
#       if long % array[-1] == 0
#         sum += array[-1]*(long/array[-1]*2-1)
#         break
#       else
#         sum += array[-1]*(long/array[-1]*2)
#         array.push(long%array[-1])
#         long = array[-4]
#         array.push(array[-2]%array[-1])
#       end
#     end
#   end
# end

def ab(long,short)
  if long % short == 0
    $sum += long*2 - short
    puts $sum
    exit
  else
    $sum += short * (long/short*2)
    short = long % short
    $array.push(short)
    bc($array[-4],short)
  end
end

def bc(long,short)
  if long % short == 0
    $sum += long*2 -short
    puts $sum
    exit
  else
    $sum += short * (long/short*2)
    ab(short, long % short)
  end
end

$array.push(x)
$array.push(n-x)

if x < m
  $sum += n
  $array.push(x)
  ab(n-x,x)
elsif x == m
  puts x*3
else
  $sum += x
  bc(x,n-x)
end
