The magnetic field $\mathbf{B}$ is not conservative (it is not even irrotational). Nevertheless, considering a small loop (of area $S$ ) with electric current $i$ (equivalent to a magnetic dipole) in uniform $\mathbf{B}$, it is defined a potential energy of the loop as $$\mathrm{U_p}={-S \,\,i}\,\, \hat{\mathbf{n}} \times \mathbf{B}$$
And the equilibrium positions are the ones with minimum $\mathrm{U_p}$.
How is this possible? Isn't that in complete contrast with the non conservativity of $\mathbf{B}$?