import sys def get(): return sys.stdin.readline().strip() def put(s): sys.stdout.write(s) def calc(a, b, n): if a + b >= n: return a + b - n return a + b def solve(): n = int(get()) k = int(get()) used = [False] * n dist = [0] * n step1 = k % n step2 = (k + 1) % n used[step1] = True used[step2] = True q = [0] * (10*n) q[0] = step1 q[1] = step2 top = 0 back = 2 dist[step1] = 1 dist[step2] = 1 while not used[0]: node = q[top] top += 1 if not used[calc(node, step1, n)]: used[calc(node, step1, n)] = True dist[calc(node, step1, n)] = dist[node] + 1 q[back] = (calc(node, step1, n)) back += 1 if not used[calc(node, step2, n)]: used[calc(node, step2, n)] = True dist[calc(node, step2, n)] = dist[node] + 1 q[back] = (calc(node, step2, n)) back += 1 put(str(dist[0])) solve()