N, K = list(map(int, input().split()))



ans = None

if N < K:

    ans = min(N, abs(N - K))

else:

    N = N % K

    ans = min(N, abs(N-K))





print(ans)
N, K = list(map(int, input().split()))



if N == K:

    ans = 0

elif N == 0:

    ans = 0

elif N > K:

    t = N % K

    ans = min(t, abs(t-K))

else:

    ans = min(N, abs(N-K))



print(ans)
N, K = list(map(int, input().split()))
ans = None
if N < K:
    ans = min(N, abs(N - K))
else:
    N = N % K
    ans = min(N, abs(N - K))
print(ans)

N, K = list(map(int, input().split()))
if N == K:
    ans = 0
elif N == 0:
    ans = 0
elif N > K:
    t = N % K
    ans = min(t, abs(t - K))
else:
    ans = min(N, abs(N - K))
print(ans)

-ans = None -if N < K: -    ans = min(N, abs(N - K)) +if N == K: +    ans = 0 +elif N == 0: +    ans = 0 +elif N > K: +    t = N % K +    ans = min(t, abs(t - K)) -    N = N % K