N, D = map(int, input().split()) import sys if N == D: print(0) sys.exit() xs = [1 for n in range(N)] #print(xs) for index, jump in enumerate(range(0, 1000, D)): point = jump%N if xs[point] is None: print(index-1) #print(xs) break xs[point] = None