eolymp
bolt
Try our new interface for solving problems
Problems

Equation

Equation

Given the equation of the form \textbf{X^N} + \textbf{Y^N} ≡ \textbf{Z^N mod M}. Required for fixed \textbf{N} and \textbf{M}, find the number of different solutions to this equation. Solution is called a triple of positive integers (\textbf{X}, \textbf{Y}, \textbf{Z}), which holds: \begin{itemize} \item \textbf{1} ≤ \textbf{X} ≤ \textbf{Y} < \textbf{M} \item \textbf{1 }≤ \textbf{Z} < \textbf{M} \item \textbf{X^N} + \textbf{Y^N} ≡ \textbf{Z^N mod M} \end{itemize} \InputFile In a single line of input file written numbers \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N}, \textbf{M} ≤ \textbf{7^7}). \OutputFile The output file output a single number - the answer to the problem.
Time limit 6 seconds
Memory limit 256 MiB
Input example #1
1 3
Output example #1
2
Author Dmitriy Zhukov
Source Winter School, Kharkov, 2011, Day 2