n, d = map(int, input().split()) s = [x for x in range(1, n+1)] s[0] = 0 cnt = 0 for i in range(1, n+1): if s[d%n] == 0: break else: s[d%n] = 0 cnt += 1 d += d print(cnt)