You are given a recurrent formula for a sequence :
where is also a recurrent sequence given by formula:
It is known that f(1) = 1, g(1) = 1.
Your task is to find modulo .
The input consists of several cases. Each case contains two numbers on a single line. These numbers are (1 \le n \le 10000) and (2 \le p \le 2 \cdot 10^9). The input is terminated by the case with n=p=0 which should not be processed. The number of cases in the input does not exceed .
Output for each case modulo on a separate line.
Example 1