A, B = [ int( n ) for n in input().split() ] for x in range( 1, 100001 ): if x % A == B % x: print( x ) break