#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:

    if n%k==0:

        ans=0

    else:

        #ans=fractions.gcd(n,k)

        ans=n%k

        templ=[]

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

else:

    #ans=fractions.gcd(n,k)

    ans=n%k

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

print(ans)



#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)
# 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:
    if n % k == 0:
        ans = 0
    else:
        # ans=fractions.gcd(n,k)
        ans = n % k
        templ = []
        ans = min(ans, abs(ans - k))
else:
    # ans=fractions.gcd(n,k)
    ans = n % k
    ans = min(ans, abs(ans - k))
print(ans)

# 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)

-    if n % k == 0: -        ans = 0 -    else: -        # ans=fractions.gcd(n,k) -        ans = n % k -        templ = [] -        ans = min(ans, abs(ans - k)) +    ans = min(n % k, abs(k - n % k)) -    # ans=fractions.gcd(n,k) -    ans = n % k -    ans = min(ans, abs(ans - k)) +    ans = min(k - n, n)