I can either tally all the atoms one by one or use my knowledge of ionic bonds to make the tally system much simpler.
I'll do the simpler version.
First re-write the chemical equation to avoid confusing myself:
H_3PO_4H3PO4 + Mg(OH)_2Mg(OH)2 = Mg_3(PO_4)_2Mg3(PO4)2 + HH-OHOH
Notice that I had shown the line structure between the H^+H+ and OH^-OH− ions. Now, let's tally based on the subscripts.
left side:
H^+H+ = 3
PO_4^"3-"PO3-4 = 1
Mg^"2+"Mg2+ = 1
OH^-OH− = 2
right side:
H^+H+ = 1
PO_4^"3-"PO3-4 = 2
Mg^"2+"Mg2+ = 3
OH^-OH− = 1
Let's start balancing the largest ion, PO_4^"3-"PO3-4.
color (red) 2 H_3PO_42H3PO4 + Mg(OH)_2Mg(OH)2 = Mg_3(PO_4)_2Mg3(PO4)2 + HH-OHOH
left side:
H^+H+ = 3 x color (red) 22 = 6
PO_4^"3-"PO3-4 = 1 x color (red) 22 = 2
Mg^"2+"Mg2+ = 1
OH^-OH− = 2
right side:
H^+H+ = 1
PO_4^"3-"PO3-4 = 2
Mg^"2+"Mg2+ = 3
OH^-OH− = 1
Do not forget that since H_3PO_4H3PO4 is a substance, you need to also multiply the coefficient with the number of HH atoms.
Next,
2 H_3PO_42H3PO4 + color (green) 3Mg(OH)_23Mg(OH)2 = Mg_3(PO_4)_2Mg3(PO4)2 + HH-OHOH
left side:
H^+H+ = 3 x 2 = 6
PO_4^"3-"PO3-4 = 1 x 2 = 2
Mg^"2+"Mg2+ = 1 x color (green) 33 = 3
OH^-OH− = 2 x color (green) 33 = 6
right side:
H^+H+ = 1
PO_4^"3-"PO3-4 = 2
Mg^"2+"Mg2+ = 3
OH^-OH− = 1
Again, since Mg(OH)_2Mg(OH)2 is a substance, whatever coefficient you apply to Mg^"2+"Mg2+ ion will also be applied to OH^-OH− ion.
2 H_3PO_42H3PO4 + 3Mg(OH)_23Mg(OH)2 = Mg_3(PO_4)_2Mg3(PO4)2 + color (blue) 66HH-OHOH
left side:
H^+H+ = 3 x 2 = 6
PO_4^"3-"PO3-4 = 1 x 2 = 2
Mg^"2+"Mg2+ = 1 x 3 = 3
OH^-OH− = 2 x 3 = 6
right side:
H^+H+ = 1 x color (blue) 66 = 6
PO_4^"3-"PO3-4 = 2
Mg^"2+"Mg2+ = 3
OH^-OH− = 1 x color (blue) 66 = 6
Reverting back to the original equation,
2 H_3PO_42H3PO4 + 3Mg(OH)_23Mg(OH)2 = Mg_3(PO_4)_2Mg3(PO4)2 + 6H_2O6H2O
The equation is now balanced.
Please take note this method is only applicable for IONIC bonding of polyatomic ions.