/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<assert.h>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 300005
#define nm 200005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
ll inf,_x,n,_y;
struct mat{int n,m;ll a[4][4];}t,ans,E;
mat operator*(const mat&x,const mat&y){
mat s;s.n=x.n;s.m=y.m;mem(s.a);
inc(i,1,s.n)inc(j,1,s.m)inc(k,1,x.m)s.a[i][j]+=x.a[i][k]*y.a[k][j]%inf,s.a[i][j]%=inf;
return s;
}
mat qpow(mat x,ll t){return t?qpow(sqr(x),t>>1)*(t&1?x:E):E;}
int main(){
E.a[1][1]=E.a[2][2]=E.a[3][3]=1;E.n=E.m=3;
n=read();inf=read();
if(n==1)return 0*printf("1\n");
t.n=t.m=3;ans.n=3;ans.m=1;
t.a[2][3]=t.a[2][2]=t.a[1][2]=t.a[1][3]=t.a[3][3]=1;
ans.a[1][1]=ans.a[2][1]=0;ans.a[3][1]=1;
//inc(i,1,t.n){inc(j,1,t.m)printf("%d ",t.a[i][j]);putchar('\n');}
//inc(i,1,E.n){inc(j,1,E.m)printf("%d ",E.a[i][j]);putchar('\n');}
//inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
for(_x=1,_y=1;_y<18&&_x*10<=n+1;_y++,_x*=10){
t.a[1][1]=_x*10%inf;
ans=qpow(t,_x*9)*ans;
//printf("ans:\n");inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
}
if(n>=_x){
t.a[1][1]=_x*10%inf;
n=n-_x+1;
ans=qpow(t,n)*ans;
}
//printf("ans:\n");inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
return 0*printf("%lld\n",ans.a[1][1]);
}