#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long euclid_extins(long long a,long long b,long long &x,long long &y) {
if(b==0)
{
x=1;
y=0;
return a;
}
long long x1,y1;
long long d=euclid_extins(b,a%b,x1,y1);
x=y1;
y=x1-(a/b)*y1;
return d;
}
int main() {
long long A,N;
fin>>A>>N;
long long x,y;
long long sol=euclid_extins(A,N,x,y);
if(sol!=1)
{
fout<<"Err";
return 1;
}
else
{
x=(x%N+N)%N;
fout<<x;
}
fin.close();
fout.close();
return 0;
}