Exponial

Compute n^(n-1)^(...^1) modulo m for n and m up to 1e9, a tower too tall to build.

Hard8Number theoryRecursionMathNo attempts yetTime limit2sMemory limit512 MB

Problem

There are many ways to build very large numbers. Two of them are:

  • Exponentiation: 422016=42×42××422016 times42^{2016} = \underbrace{42 \times 42 \times \cdots \times 42}_{2016 \text{ times}}
  • Factorials: 2016!=2016×2015××2×12016! = 2016 \times 2015 \times \cdots \times 2 \times 1

This problem uses their lesser known combination, the exponial, which is defined for every positive integer nn as

exponial(n)=n(n1)(n2)21\mathrm{exponial}(n) = n^{(n-1)^{(n-2)^{\cdots^{2^1}}}}

For example, exponial(1)=1\mathrm{exponial}(1) = 1 and exponial(5)=543216.206×10183230\mathrm{exponial}(5) = 5^{4^{3^{2^1}}} \approx 6.206 \times 10^{183230}, which is already very large. Exponentiation is right associative, so abc=a(bc)a^{b^c} = a^{(b^c)}.

Exponials are too large to handle directly. Write a program that computes the remainder of exponial(n)\mathrm{exponial}(n) divided by mm.

Input

The first line contains two integers nn and mm separated by a space (1n1091 \le n \le 10^9, 1m1091 \le m \le 10^9).

Output

Print the remainder of exponial(n)\mathrm{exponial}(n) divided by mm on one line.