Codeforces Round 140 (Div. 1)
A
Flying Saucer Segments
Codeforces
1400
原题
←B →
翻译中…

An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).

翻译中…

The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one — to the 1-st and the 3-rd ones, the 3-rd one — only to the 2-nd one. The transitions are possible only between the adjacent sections.

翻译中…

The spacecraft team consists of $*n*$ aliens. Each of them is given a rank — an integer from $1$ to $*n*$. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section $*a*$ to section $*b*$ only if it is senior in rank to all aliens who are in the segments $*a*$ and $*b*$ (besides, the segments $*a*$ and $*b*$ are of course required to be adjacent). Any alien requires exactly $1$ minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.

翻译中…

Alien $*A*$ is senior in rank to alien $*B*$, if the number indicating rank $*A*$, is more than the corresponding number for $*B*$.

翻译中…

At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.

翻译中…

Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo $*m*$.

翻译中…

Input

翻译中…

The first line contains two space-separated integers: $*n*$ and $*m*$ $(1 ≤ *n*, *m* ≤ 10^{9})$ — the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.

翻译中…

Output

翻译中…

Print a single number — the answer to the problem modulo $*m*$.

翻译中…

Note

翻译中…

In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.

翻译中…

To briefly describe the movements in the second sample we will use value image, which would correspond to an alien with rank $*i*$ moving from the segment in which it is at the moment, to the segment number $*j*$. Using these values, we will describe the movements between the segments in the second sample: image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image; In total: the aliens need 26 moves. The remainder after dividing $26$ by $8$ equals $2$, so the answer to this test is $2$.

翻译中…

样例

Input #1
1 10
Output #1
2
InputCopy OutputCopy
Input #2
3 8
Output #2
2
InputCopy OutputCopy
题目信息
题号
226A
时间限制
2 s
内存限制
256 MB
标签
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。