def solve(a,b):
	a,b = max(a,b),min(a,b)
	if b == 0:
		return 0
	res = 2*a
	r = solve(b-a%b, a%b)
	if r == 0:
		res -= b
	res += r
	return res
	
n,x = map(int, raw_input().split())

ans = n + solve(x, n-x)
print ans
