#include<iostream>
#include<cstdio>
using namespace std;
class COUPLE{
public:
long long n1,n2;//单纯的数对!!!!!!!
};
COUPLE extended_gcd(long long a,long long b){
//ax+by=1的最小整数解(x,y)
long long t;
COUPLE temp;
if(b==0){
temp.n1=1,temp.n2=0;
return temp;
}
COUPLE before;
before=extended_gcd(b,a%b);
t=before.n1;
temp.n1=before.n2;
temp.n2=before.n1-(a/b)*before.n2;
return temp;
}
int main(){
freopen("mod.in","r",stdin);
freopen("mod.out","w",stdout);
long long a,b;
cin>>a>>b;
COUPLE ans=extended_gcd(a,b);
ans.n1=(ans.n1+b)%b;
cout<<ans.n1<<endl;
return 0;
}