#n=int(input())

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

#l=list(map(int,input().split()))

#l=[list(map(int,input().split())) for i in range(n)]

ans=0

import fractions

newval=n%k

if n>=k:

    ans=min(n%k,abs(k-n%k))

else:

    ans=min(k-n,n)

print(ans)
#k=int(input())

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

#hl=list(map(int,input().split()))

#l=[list(map(int,input().split())) for i in range(n)]

t=n%k

print((min(t,k-t)))



# n=int(input())
n, k = list(map(int, input().split()))
# l=list(map(int,input().split()))
# l=[list(map(int,input().split())) for i in range(n)]
ans = 0
import fractions

newval = n % k
if n >= k:
    ans = min(n % k, abs(k - n % k))
else:
    ans = min(k - n, n)
print(ans)

# k=int(input())
n, k = list(map(int, input().split()))
# hl=list(map(int,input().split()))
# l=[list(map(int,input().split())) for i in range(n)]
t = n % k
print((min(t, k - t)))

-# n=int(input()) +# k=int(input()) -# l=list(map(int,input().split())) +# hl=list(map(int,input().split())) -ans = 0 -import fractions - -newval = n % k -if n >= k: -    ans = min(n % k, abs(k - n % k)) -else: -    ans = min(k - n, n) -print(ans) +t = n % k +print((min(t, k - t)))