Potential energy of a magnetic dipole in a uniform magnetic field $$U=-MB\cos q$$ clearly the potential energy is maximum when $$\cos \theta =-1$$ or $$\theta =\pi$$ That is, potential energy is maximum when magnetic dipole with its magnetic moment $$\vec M$$ is oriented opposite to the direction of magnetic field ( or angle between $$\vec M$$ and $$\vec \delta $$ is $$180^o)$$.