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


def solve(edge, width, ans):
	a, b = divmod(width, edge)
	if b == 0:
		return ans+edge*(2*a-1)
	return solve(width-edge*a, edge, ans + edge*2*a)

print(solve(min(X,N-X), max(X, N-X), N))



