There are 3 buckets labelled (small) of 3 litres, (medium) of 5 litres and (large) of 8 litres. Initially and are full of water and is empty.By pouring water between the buckets exactly one litre of water can be measured. Since there is no other way to measure, once a pouring starts it cannot stop until either the source bucket is empty or the destination bucket is full. At least four pourings are needed to get one litre:
After these operations, there is exactly one litre in bucket .
In general the sizes of the buckets are , , litres, respectively. Initially and are full and is empty. If the above rule of pouring still applies and and are two coprime positive integers with then it is always possible to measure one litre in finitely many steps.
Let be the minimal number of pourings needed to get one litre. Thus . Also, and .
Find the sum of for all pairs of prime numbers such that . Give your answer modulo .