Before the gun was fired, neither mass had any velocity and therefore the total momentum P = P1 + P2 was zero. Afterwards, evidently, the total momentum remained zero. This is a general property (and yet another formulation of Newton's laws) and can be stated as
In a system of objects subject to no forces from the outside, the vector sum of all momenta stays the same ("is conserved").
This also works when three or more objects are involved and each moves along a completely different direction. For instance, "the shells bursting in the air" of the US anthem had the same momentum as the collection of fragments and gases produced imediately after they burst, before air resistance had its say. This is also the principle by which a rocket operates--as it throws mass backwards in a fast jet of gas, it receives an amount of forward momentum equal to the backward momentum given to the jet.
When the cannon recoils, it receives as much momentum as the shell. How is the energy E shared? Since
E = mv2/2
we have, for the shell
E1 = 10 kg (1000m/s)2/2 = 5,000,000 joule
and for the cannon
E2 = 1000 kg (10 m/s)2/2 = 50,000 joule
Very unequal sharing! The cannon, 100 times massive, receives 100 times less energy. Is that the rule?
Yes, it is. We have, using symbols
E1 = m1v12/2
E2 = m2v22/2
E1/E2 = m1v12/m2v22
Substitute equation (1) in the numerator, then cancel (m2v2) above and below
E1/E2 = v1/v2
By (4), invertedv1/v2 = m2/m1
hence E1/E2 = m2/m1
The lighter mass always receives the lion's share of the energy!