/*
TASK: fib
LANG: C++
*/
#include<iostream>
using namespace std;
    long long ost[1000000]={0,1,1};
int main()
{
    long long n,m,tmp;
    int i=3;
    scanf("%lld%lld",&n,&m);
    if(n<3){printf("%lld\n",ost[n]%m);return 0;}
    while(ost[i-1]!=0 || ost[i-2]!=1 && i<1000000)
    {
     tmp=(ost[i-1]+ost[i-2])%m;
     ost[i]=tmp;
     i++;
    }
    i--;
    if(n%i==0){printf("%lld\n",ost[i]);return 0;}
    printf("%lld\n",ost[n%i]);
return 0;
}

