Description:

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 aand 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 ≤ 109)— 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.

Examples

Input


1 10


Output


2


Input


3 8


Output


2


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 

CodeForces - 227C  Flying Saucer Segments(递推)_#include

, 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: 

CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_03


CodeForces - 227C  Flying Saucer Segments(递推)_取模_04


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_06


CodeForces - 227C  Flying Saucer Segments(递推)_#include_07


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_03


CodeForces - 227C  Flying Saucer Segments(递推)_ide_10


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_06


CodeForces - 227C  Flying Saucer Segments(递推)_取模_04


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_03


CodeForces - 227C  Flying Saucer Segments(递推)_ide_16


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_06


CodeForces - 227C  Flying Saucer Segments(递推)_ide_19


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_03


CodeForces - 227C  Flying Saucer Segments(递推)_取模_04


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_06


CodeForces - 227C  Flying Saucer Segments(递推)_#include_07


CodeForces - 227C  Flying Saucer Segments(递推)_#include_02


CodeForces - 227C  Flying Saucer Segments(递推)_#include_03

; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.

这道题类似汉诺塔问题,有三个飞船只能从3走到2,从2走到1,,并且如果想移动一个人,那么只能移动初始位置级别最高的那个人。所有人的初始位置在3,要使全部人都走到1,求最小走的步数。

这道题其实和汉诺塔问题十分相似,假如有两个人,只能先让2从3到2,然后从2到1,再让1从3到2,现在只能让2从1到3,然后1从2到1,再移动2从3到1,这样就是3+2+3一个8步。

三个人就是把12 看做一个整体,类似以上操作总步数就是3*(2的总步数)+2,因为每一次的操作都会影响上次的总步数。

可以得到递推关系式放f(n)=3*f(n-1)+2;

这样写出递推代码打表一下

CodeForces - 227C  Flying Saucer Segments(递推)_ide_28

可以发现规律答案就是3^n-1然后取模。

取模的时候注意要防止为负数的情况

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<stack>
#include<vector>
#include<map>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k,l;
int n;
ll m,q;
int p;
int c,d;
int cnt;
ll ans;
int main()
{
cin>>n>>p;
int a=3;
ans=1%p;
while(n)
{
if(n&1)
ans=ans*a%p;
a=a*1ll*a%p;
n>>=1;
}
ans--;
cout<<(ans+p)%p<<endl;
return 0;
}