def f(a,b):
	return 2*a + f_fast(a,b-a)



def f_fast(a,b):
	if a==b:
		return a+b
	elif a < b:
		if b%a == 0:
			return f(a,b)
		else:
			d = b//a
			return 2*a*d + f_fast(a,b%a)
	else:
		return f_fast(b,a)


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

print(N+f_fast(N-X,X))