def E(a,b): if b:u,v,w=E(b,a%b);return u,w,v-a/b*w return a,1,0 while 1:a,m=map(int,raw_input().split());print E(a,m)[1]%m