D. Saki and Railway Construction
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

As the head of the Ethics community, Saki is charged with overseeing the railway construction over $$$N+1$$$ sites, numbered from $$$0$$$ to $$$N$$$. It is known that for some unknown positive integer $$$x$$$, geotechnical engineers figured out the following list of candidate bidirectional paths that railways can be constructed upon.

  • For each integer $$$1 \le i \lt N$$$, there is one candidate between the $$$i$$$-th and $$$i+1$$$-th site.
  • There are $$$x-1$$$ candidates between $$$0$$$-th and $$$1$$$-st site.
  • For each integer $$$2 \le i \lt N$$$, there are $$$2x-2$$$ candidates between $$$0$$$-th and $$$i$$$-th site.
  • There are $$$2x-1$$$ candidates between $$$0$$$-th and $$$N$$$-th site.

Saki is planning to pick $$$N$$$ of the candidates and build railways so that all $$$N+1$$$ sites are connected either directly or indirectly. She has countably-infinite many friends, numbered $$$1, 2, 3, \cdots$$$. She asked the friend numbered $$$1$$$ to compute the number of ways to pick such a set of candidates whenever Saki choose the value $$$x$$$. Let the correct answer be $$$f(x)$$$.

To make sure, each friend $$$i$$$ asked the friend $$$i+1$$$ to double-check the computation. However, due to a mistake in their communication, when friend $$$i$$$ received the integer $$$y$$$, he/she passed the value $$$f(y)$$$ to friend $$$i+1$$$ instead of the value $$$y$$$. Therefore, friend 1 computed $$$f(x)$$$, friend 2 computed $$$f(f(x))$$$, the friend 3 computed $$$f(f(f(x)))$$$, and so on.

However, one of Saki's friend Satoru noticed that regardless of the value $$$x$$$ Saki chose, for some fixed prime $$$M$$$, his computation result was always equal to $$$x$$$ modulo $$$M$$$. Satoru now wonders what his friend number would be, which is a positive integer, unless there were some mistakes in one of the computations.

Given $$$N$$$ and $$$M$$$, write a program which determines whether this was possible, and if possible, find the minimum friend number of Satoru, modulo $$$998\,244\,353$$$.

Input

The input is given in the following format:

$$$N$$$$$$M$$$

where $$$N+1$$$ is the number of construction sites, and $$$M$$$ is the prime modulus of Satoru.

The input satisfies the following constraints:

  • All numbers in the input are integers.
  • $$$2 \le N \le 1\,000\,000\,000\,000\,000\,000$$$ ($$$= 10^{18}$$$)
  • $$$2 \le M \le 1\,000\,000\,000$$$ ($$$= 10^{9}$$$)
  • $$$M$$$ is a prime.
Output

If there exists a positive integer $$$K$$$ which could have been the friend number of Satoru, print a single line containing the minimum such $$$K$$$, modulo $$$998\,244\,353$$$. Otherwise, print a single line containing $$$-1$$$.

Examples
Input
29 89
Output
30
Input
2 2
Output
-1