If the magnet be rotated from an initial orientation $\theta_{1}=0^{\circ}$ to final orientation $\theta_{2}=180^{\circ}$ the total work done
$W =\int\limits_{\theta_{1}}^{\theta_{2}} M B \sin d \theta $
$=M B[-\cos \theta]_{\theta_{1}}^{\theta_{2}} $
$=M B\left(\cos \theta_{1}-\cos \theta_{2}\right)$
where $B=$ magnetic field induction
$M =$ magnetic moment
$=M B\left(+\cos 0^{\circ}-\cos 180^{\circ}\right) $
$=M B[1+1]=2 \,M B$