Sum of Powers#11777

Sum of Powers

5 seconds
128 megabytes
Medium

Let be the sum of the digits of a non-negative integer . For example, s(123) = 1 + 2 + 3 = 6.

Define the following function:

f(n) = \begin{cases} n, if~n~is~prime \\ s(n), if~n~is~composite \end{cases}

Find the value of the following sum:

S = \left( \sum_{i = 2}^n i^{f(i)}\right) \mod m

where is a given number.

Input

Two positive integers n~(2 \le n \le 10^7) and m~(m \le 10^9).

Output

Print the value of the desired sum.

Examples

For n = 5 the answer is S = 2^2 + 3^3 + 4^4 + 5^5 = 3412. Note that the number is composite, and the sum of its digits is s(4) = 4.

Example 1

Loading
Loading