#include<cstdio> #include<iostream> #include<cstdlib> #include<iomanip> #include<cmath> #include<cstring> #include<string> #include<algorithm> using namespace std; long long x,y,m,n,L; void exgcd(long long a,long long b,long long &d,long long &x,long long &y) { long long t; if(b==0) { d=a;x=1;y=0; } else { exgcd(b,a%b,d,x,y); t=x;x=y;y=t-(a/b)*y; } } int main() { long long a,b,d,x,y; cin>>a>>b; exgcd(a,b,d,x,y); cout<<(x/d%(b/d)+b/d)%(b/d)<<endl; return 0; }