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

ans = N

while True:
	if X == Y:
		ans += X
		break
	else:
		if X > Y:
			tmp = X
			X = Y
			Y = tmp
		a = Y % X
		b = Y // X
		if a == 0:
			ans += (b*2 - 1) * X
			break
		else:
			ans += b*2*X
		Y = X
		X = a
print(ans)
