NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
N =NX[0]
X =NX[1]
D = NX[0]-NX[1]
A=X+D
def ans(x,y):
	global A
	A +=int(x/y)*y+y
	if x%y==0:
		return print(A)
	ans(y,x%y)

if X>D:
	ans(X,D)
else:	ans(D,X)
