#The solution in Python 3 is as follows:
from fractions import gcd
N, M = map(int, input().split())
print (M - gcd(M, N)) #simple formula for output
#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!
from fractions import gcd
N, M = map(int, input().split())
print (M - gcd(M, N)) #simple formula for output
#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!