In both cases less energy is required
But comparetively Mg require more energy than K
Let's see the electron configuration of Both
K has only one valence electron so very less ionization enthalpy so less energy required
Mg has 2 so more IE hence more energy required