noip2012
include<bits/stdc++.h>
using namespace std; long long a , b;
long long ansx , ansy;
void exgcd(int x ,int y){
if(y == 0){
ansx = 1 , ansy = 0;
return ;
}
exgcd(y , x % y);
long long nowx = ansx;
ansx = ansy , ansy = nowx - (x / y) * ansy;
// cout<<ansx<<' '<<ansy<<endl;
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b;
exgcd(a , b);
while(ansx < 0){
ansx += b;
}
ansx %= b;
cout<<ansx;
return 0;
};