#include <iostream>
#include <vector>
int main()
{
unsigned long long n = 0, m = 0;
std::cin >> n >> m;
std::vector<unsigned long> mod;
mod.push_back(0);
mod.push_back(1);
unsigned long k = 0;
for (unsigned long i = 2; i <= n; ++i)
{
++k;
mod.push_back((mod[i - 1] + mod[i - 2]) % m);
if (mod[i - 1] == 0 && mod[i] == 1)
break;
}
std::cout << mod[(n%k)] << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKQp7Cgl1bnNpZ25lZCBsb25nIGxvbmcgbiA9IDAsIG0gPSAwOwoJc3RkOjpjaW4gPj4gbiA+PiBtOwoJc3RkOjp2ZWN0b3I8dW5zaWduZWQgbG9uZz4gbW9kOwoJbW9kLnB1c2hfYmFjaygwKTsKCW1vZC5wdXNoX2JhY2soMSk7Cgl1bnNpZ25lZCBsb25nIGsgPSAwOwoJZm9yICh1bnNpZ25lZCBsb25nIGkgPSAyOyBpIDw9IG47ICsraSkKCXsKCQkrK2s7CgkJbW9kLnB1c2hfYmFjaygobW9kW2kgLSAxXSArIG1vZFtpIC0gMl0pICUgbSk7CgkJaWYgKG1vZFtpIC0gMV0gPT0gMCAmJiBtb2RbaV0gPT0gMSkKCQkJYnJlYWs7Cgl9CglzdGQ6OmNvdXQgPDwgbW9kWyhuJWspXSA8PCBzdGQ6OmVuZGw7CglyZXR1cm4gMDsKfQ==