1 #include<cstdio>
2 void gcd(int x,int y,int &a,int &b,int &c){
3 if(!y){a=x;b=1;c=0;return;}
4 gcd(y,x%y,a,c,b);
5 c-=x/y*b;
6 }
7 int main(){
8 int n,k,a,b,c;
9 scanf("%d%d",&n,&k);
10 gcd(n,k,a,b,c);
11 b=(b%k+k)%k;
12 printf("%d",b);
13 return 0;
14 }