#include<bits/stdc++.h> using namespace std; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; } else{ exgcd(b, a%b, y, x); y-=x*(a/b); } } int main() { long long a,b; cin>>a>>b; int x,y; exgcd(a,b,x,y); printf("%d\n",((x%b)+b)%b); return 0; }