They:
Exchange their treasures one coin/gem at a time - this way, any losses are minimized.
Alternatively:
We wish to swap the values of P and M without either ever having more than the other (and thus running off with all the loot!). This is IMPOSSIBLE - the only swap routines using only two variables invariably create the scenario with one variable greater than the other. For example, the XOR swap algorithm.