n,x = gets.split.map(&:to_i)

def para(a,b)
  short,long = [a,b].sort
  if short == long
    return short
  elsif long % short == 0
    return long*2 - short
  elsif short < long
    return short * 2 + para(long-short,short)
  end
end

puts n + para(x,n-x)
