#include using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define pb push_back #define MOD 1000000007 //998244353 #define PI 3.141592653 #define INF 100000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); vector> mat_mul(vector> a, vector> b, ll m) { vector> c(a.size(),vector(b[0].size(),0)); REP(i,a.size()) REP(k,b[0].size()) REP(j,b.size()) { c[i][k]=(c[i][k]+(a[i][j]*b[j][k])%m)%m; } return c; } vector> mat_pow(vector> x, ll n, ll m) { vector> y(x.size(),vector(x.size(),0)); REP(i,x.size()) y[i][i]=1; while (n) { if (n&1) y=mat_mul(x,y,m); x = mat_mul(x,x,m); n>>=1; } return y; } int main(){ ll n, m; cin >> n >> m; vector> fib = {{0,1}}; vector> t = {{0,1},{1,1}}; vector> y = mat_pow(t,n-1,m); // cout << "test" << endl; // cout << y[0][0] << " " << y[0][1] << endl; // cout << y[1][0] << " " << y[1][1] << endl; y = mat_mul(fib,y,m); cout << y[0][0] << endl; }