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

import math

if N > K:

    s = N // K

    print((min(abs(N - s * K), abs(N - (s + 1) * K))))

elif N == K:

    print((0))

else:

    print((min(N, abs(N - K))))
N, K = list(map(int, input().split()))

s = N // K

ans = min(abs(N - s * K), abs(N - (s + 1) * K))

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

if N > K:
    s = N // K
    print((min(abs(N - s * K), abs(N - (s + 1) * K))))
elif N == K:
    print((0))
else:
    print((min(N, abs(N - K))))

N, K = list(map(int, input().split()))
s = N // K
ans = min(abs(N - s * K), abs(N - (s + 1) * K))
print(ans)

-import math - -if N > K: -    s = N // K -    print((min(abs(N - s * K), abs(N - (s + 1) * K)))) -elif N == K: -    print((0)) -else: -    print((min(N, abs(N - K)))) +s = N // K +ans = min(abs(N - s * K), abs(N - (s + 1) * K)) +print(ans)