Given is a functionf: 0..N --> 0..N
for a non-negativeN
and a non-negative integern
. One can construct an infinite sequenceF = f 1(n), f 2(n), ... f k(n) ...
, wheref k(n)
is defined recursively as follows:f 1(n) = f(n)
It is easy to see that each such sequence F is eventually periodic, that is periodic from some point onwards, e.g 1, 2, 7, 5, 4, 6, 5, 4, 6, 5, 4, 6 ... . Given non-negative integer N ≤ 11000000 , n ≤ N and f, you are to compute the period of sequence F.
Each line of input contains N, n and the a description of f in postfix notation, also known as Reverse Polish Notation (RPN). The operands are either unsigned integer constants or N or the variable x. Only binary operands are allowed: + (addition), * (multiplication) and % (modulo, i.e. remainder of integer division). Operands and operators are separated by whitespace. The operand % occurs exactly once in a function and it is the last (rightmost, or topmost if you wish) operator and its second operand is always N whose value is read from input. The following function:
2 x * 7 + N %
is the RPN rendition of the more familiar infix(2*x+7)%N
. All input lines are shorter than 100 characters. The last line of input hasN
equal 0 and should not be processed.
For each line of input, output one line with one integer number, the period of F corresponding to the data given in the input line.