import sys

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



ans = N % K

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

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



print((min(N%K, K-N%K)))
import sys

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

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

-import sys - -ans = N % K -ans = min(ans, abs(K - ans)) -print(ans) +print((min(N % K, K - N % K)))