Solution (source code)

= Solution

The time-independent <Lagrangian> conserves $E=T+V$. Substitute $\dot x=-m_2l\dot\phi\cos\phi/M$ into the <kinetic energy> to obtain
$$
\boxed{E=\frac{m_2l^2}{2M}(m_1+m_2\sin^2\phi)\dot\phi^2-m_2gl\cos\phi.}
$$
This is the angle-dependent effective inertia of a <pendulum with a freely sliding support>. Solving for the speed gives
$$
|\dot\phi|=\sqrt{\frac{2M(E+m_2gl\cos\phi)}{m_2l^2(m_1+m_2\sin^2\phi)}}.
$$
On a monotone increasing segment, the travel time is therefore
$$
\boxed{t=l\sqrt{\frac{m_2}{2M}}\int_{\phi_1}^{\phi_2}
\sqrt{\frac{m_1+m_2\sin^2\phi}{E+m_2gl\cos\phi}}\,d\phi.}
$$
The printed oriented <integral> assumes that increasing-angle branch. For decreasing motion use $|d\phi|$ or interchange the limits; if a <turning point> lies between the two events, split the trajectory into monotone segments and add their positive times. The numerator remains positive for positive masses. At an ordinary <turning point> the inverse-square-root singularity is integrable; reaching the unstable upright <separatrix> takes infinite time.