分段矩乘即可
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# define ll long long
# define RG register
# define IL inline
# define UN unsigned
# define mem(a, b) memset(a, b, sizeof(a))
# define min(a, b) ((a) < (b)) ? (a) : (b)
# define max(a, b) ((a) > (b)) ? (a) : (b)
using namespace std;
ll m;
struct Matrix{
ll a[3][3];
IL void Clear(){mem(a, 0);}
IL void First(){a[0][0] = a[1][1] = a[2][2] = 1;}
IL Matrix operator *(Matrix &B){
RG Matrix C; C.Clear();
for(RG int i = 0; i < 3; i++)
for(RG int j = 0; j < 3; j++)
for(RG int k = 0; k < 3; k++){
C.a[i][j] += (a[i][k] * B.a[k][j]) % m;
C.a[i][j] %= m;
}
return C;
}
} S, T[19];
IL ll Get(){
RG char c = '!'; RG ll z = 1, num = 0;
while(c != '-' && (c < '0' || c > '9'))
c = getchar();
if(c == '-')
z = -1, c = getchar();
while(c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num * z;
}
IL void Pow(RG ll n, RG int x){
while(n){
if(n & 1) T[x] = T[x] * S;
S = S * S;
n >>= 1;
}
}
int main(){
RG ll n = Get();
RG UN ll t = n;
m = Get();
RG int k = 0;
while(t) t /= 10, k++;
t = 1;
for(RG int i = 1; i < k; i++){
S.Clear();
t *= 10; S.a[0][0] = t % m; T[i].First();
S.a[1][0] = S.a[1][1] = S.a[2][0] = S.a[2][1] = S.a[2][2] = 1;
Pow(t - t / 10, i);
}
S.Clear();
t *= 10; S.a[0][0] = t % m; T[k].First();
S.a[1][0] = S.a[1][1] = S.a[2][0] = S.a[2][1] = S.a[2][2] = 1;
Pow(n - t / 10 + 1, k);
S.Clear(); S.First();
for(RG int i = 1; i <= k; i++)
S = S * T[i];
printf("%d
", S.a[2][0] % m);
return 0;
}