import sys
import heapq
sys.setrecursionlimit(10**8)


#最大公約数
def gcd(a,b):
	while b:
		a,b = b, a%b
	return a
	
#最小公倍数
def lcm(a,b):
	return a*b // gcd(a,b)



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

ans = N

a = min(X,N-X)
b = max(X,N-X)

while True:	#a<b
	n = b//a 
	ans += n * 2 * a
	
	if n*a == b:
		ans -= a
		print(ans)
		sys.exit()
		
		
	a, b = b-n*a, a