#

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

N  %= K

min = N

for i in range(10**6):

    N = abs(N-K)

    if min > N:

        min = N

    if N == 0:

        min = 0

        break

print(min)
#input

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



#main

t = N % K

ans = min(t, K - t)



#output

print(ans)
#
N, K = list(map(int, input().split()))
N %= K
min = N
for i in range(10**6):
    N = abs(N - K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

# input
N, K = list(map(int, input().split()))
# main
t = N % K
ans = min(t, K - t)
# output
print(ans)

-# +# input -N %= K -min = N -for i in range(10**6): -    N = abs(N - K) -    if min > N: -        min = N -    if N == 0: -        min = 0 -        break -print(min) +# main +t = N % K +ans = min(t, K - t) +# output +print(ans)