N, K = [int(_) for _ in input().split()]

N = N % K

if N * 2 > K:

    print((K - N))

else:

    print(N)

# N = int(input())

N, K = [int(_) for _ in input().split()]

N = N % K

print((min(abs(N-K), N)))

N, K = [int(_) for _ in input().split()]
N = N % K
if N * 2 > K:
    print((K - N))
else:
    print(N)

# N = int(input())
N, K = [int(_) for _ in input().split()]
N = N % K
print((min(abs(N - K), N)))

+# N = int(input()) -if N * 2 > K: -    print((K - N)) -else: -    print(N) +print((min(abs(N - K), N)))