N, X = gets.split(/ /).collect{|n| n.to_i}

sum = X

form = :t
a = X
b = N - X

while a > 0
	if form == :t
		sum += b
		form = :p
	else
		if a > b
			form = :t
			sum += b
			a -= b
		else
			form = :t
			sum += a
			next_a = b - a
			b = a
			a = next_a
		end
	end
end
puts sum

