n,k=list(map(int,input().split()))

if n>k:

    n=n-n//k*k

ans=10**18+10

for i in range(10000):

    ans=min(ans,n)

    n=abs(n-k)

print(ans)
n,k=list(map(int,input().split()))

print((min(n%k,k-n%k)))
n, k = list(map(int, input().split()))
if n > k:
    n = n - n // k * k
ans = 10**18 + 10
for i in range(10000):
    ans = min(ans, n)
    n = abs(n - k)
print(ans)

n, k = list(map(int, input().split()))
print((min(n % k, k - n % k)))

-if n > k: -    n = n - n // k * k -ans = 10**18 + 10 -for i in range(10000): -    ans = min(ans, n) -    n = abs(n - k) -print(ans) +print((min(n % k, k - n % k)))