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

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

def bc(long,short)
  $array.push(long % short)
  if long % short == 0
    $sum += long*2
    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
