#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n, p;
std::cin >> n >> p;
std::vector<int> l;
int r = n;
while (true) {
r = (r * n) % p;
auto it = std::find(l.begin(), l.end(), r);
if (it != l.end()) {
std::cout << l.size() - std::distance(l.begin(), it) << std::endl;
break;
}
l.push_back(r);
}
return 0;
}