f=lambda a,b:a<2or b-b*f(b%a,a)/a while 1:print f(*map(int,raw_input().split()))