For the function $$ g(a) = \underbrace{\max}_{x\in R} \left\{ \cos x + \cos \left(x + \frac{\pi}{6} \right)+ \cos \left(x + \frac{\pi}{4} \right) + cos(x + a) \right\},$$ let $b \in R$ be the input that maximizes $g$. If $\cos^2 b = \frac{m+\sqrt{n}+\sqrt{p}-\sqrt{q}}{24}$ for positive integers $m, n, p, q$, find $m + n + p + q$.

Let's think step by step and output the final answer within \boxed{}.
user