#include <iostream>
#include <vector>
#include <climits>
#include <cstdint>
using namespace std;
#define DEBUG std::cout<<"LINE"<<__LINE__;
#define IN(A) uint64_t A;std::cin>>A;
typedef complex<int64_t> P;
#define X real()
#define Y imag()
template<typename T> void printV(vector<T> V) {
for (int i=0;i<V.size()-1;i++) {
cout<<V[i]<<" ";
}
cout<<V[V.size()-1];
cout<<endl;
}
int64_t max3(int64_t a,int64_t b,int64_t c) {
return max(a,max(b,c));
}
struct compute {
int tests=1;
vector<int> data;
vector<pair<int,int>> result;
vector<bool> visited;
bool start() {
IN(a);
IN(M);
uint64_t b=a;
for (uint64_t i=1;i<M;i++) {
if (b==1) {
cout<<i<<endl;
break;
}
b=(b+a)%M;
}
if (b!=1) cout<<-1<<endl;
return true;
}
void readV(int n) {
for (int i=0;i<n;i++) cin>>data[i];
}
void testloop(){
while (tests-->0) {
start();
}
}
};
int main() {
ios_base::sync_with_stdio(false);
// cin.tie(NULL);
compute comp;
comp.start();
}