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_4# + #Mg(OH)_2# = #Mg_3(PO_4)_2# + #H#-#OH#
Notice that I had shown the line structure between the #H^+# and #OH^-# ions. Now, let's tally based on the subscripts.
left side:
#H^+# = 3
#PO_4^"3-"# = 1
#Mg^"2+"# = 1
#OH^-# = 2
right side:
#H^+# = 1
#PO_4^"3-"# = 2
#Mg^"2+"# = 3
#OH^-# = 1
Let's start balancing the largest ion, #PO_4^"3-"#.
#color (red) 2 H_3PO_4# + #Mg(OH)_2# = #Mg_3(PO_4)_2# + #H#-#OH#
left side:
#H^+# = 3 x #color (red) 2# = 6
#PO_4^"3-"# = 1 x #color (red) 2# = 2
#Mg^"2+"# = 1
#OH^-# = 2
right side:
#H^+# = 1
#PO_4^"3-"# = 2
#Mg^"2+"# = 3
#OH^-# = 1
Do not forget that since #H_3PO_4# is a substance, you need to also multiply the coefficient with the number of #H# atoms.
Next,
#2 H_3PO_4# + #color (green) 3Mg(OH)_2# = #Mg_3(PO_4)_2# + #H#-#OH#
left side:
#H^+# = 3 x 2 = 6
#PO_4^"3-"# = 1 x 2 = 2
#Mg^"2+"# = 1 x #color (green) 3# = 3
#OH^-# = 2 x #color (green) 3# = 6
right side:
#H^+# = 1
#PO_4^"3-"# = 2
#Mg^"2+"# = 3
#OH^-# = 1
Again, since #Mg(OH)_2# is a substance, whatever coefficient you apply to #Mg^"2+"# ion will also be applied to #OH^-# ion.
#2 H_3PO_4# + #3Mg(OH)_2# = #Mg_3(PO_4)_2# + #color (blue) 6##H#-#OH#
left side:
#H^+# = 3 x 2 = 6
#PO_4^"3-"# = 1 x 2 = 2
#Mg^"2+"# = 1 x 3 = 3
#OH^-# = 2 x 3 = 6
right side:
#H^+# = 1 x #color (blue) 6# = 6
#PO_4^"3-"# = 2
#Mg^"2+"# = 3
#OH^-# = 1 x #color (blue) 6# = 6
Reverting back to the original equation,
#2 H_3PO_4# + #3Mg(OH)_2# = #Mg_3(PO_4)_2# + #6H_2O#
The equation is now balanced.
Please take note this method is only applicable for IONIC bonding of polyatomic ions.