#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define ll long long using namespace std; ll n,p; void exgcd(ll a,ll b,ll &x,ll &y) { if(!b) { x=1;y=0; return; } exgcd(b,a%b,x,y); ll tmp=x; x=y; y=tmp-a/b*y; } int main() { scanf("%lld%lld",&n,&p); ll x=0,y=0; exgcd(n,p,x,y); x=(x%p+p)%p; printf("%lld ",x); return 0; }