N, D = map(int, input().split()) F = [True] * N now = 0 cnt = 0 F[now] = False while F[(now + D) % N]: cnt += 1 now = (now + D) % N F[now] = False print(cnt)