N, X = map(int, input().split())
Y = N - X

if 2 * X == N:
	print(X*3)
elif 2 * X < N:
	ans = Y + 3 * X
	a = N-2*X
	b = X
	# tmp = min(N-2*X, N-Y)
	if a < b:
		ans += ((X // a) * 2 - 1) * a
	else:
		ans += ((X // b) * 2 - 1) * b
	raise NameError('NG!')
	print(ans)
else:  # 2 * X < N
	ans = X
	ans += ((X // Y) * 2) * Y
	print(ans)
