The first fact you should know to solve a stoichiometric problem like this one is that, there are 6.02 x 10^23 particles (atoms or molecules) in 1 mole of a substance.
Now, to know the equivalence factor, it is important to know a technique which cancels like units when they are placed diagonally. Hence, to find the moles Mg from atoms, the equivalence factor must have a numerator with units of moles and a denominator with units of atoms. In this case, atoms would be cancelled, leaving the moles.