a, b = map(int, input().split()) x=1 while x%a != b%x: x += 1 print(x)