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



"""

while N > K:

  N = abs(N - K)

  #print(N)



print(min(N, abs(N-K)))

"""

"""

m = -(-N//K)

print(abs(N - m*K))

"""



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



"""

while N > K:

  N = abs(N - K)

  #print(N)



print(min(N, abs(N-K)))

"""



m = N//K

print((min(N - m*K, -(N - (m+1)*K))))



"""

print(min(N%K, abs(N%K - K)))

"""
N, K = list(map(int, input().split()))
"""
while N > K:
  N = abs(N - K)
  #print(N)
print(min(N, abs(N-K)))
"""
"""
m = -(-N//K)
print(abs(N - m*K))
"""
print((min(N % K, abs(N % K - K))))

N, K = list(map(int, input().split()))
"""
while N > K:
  N = abs(N - K)
  #print(N)
print(min(N, abs(N-K)))
"""
m = N // K
print((min(N - m * K, -(N - (m + 1) * K))))
"""
print(min(N%K, abs(N%K - K)))
"""

+m = N // K +print((min(N - m * K, -(N - (m + 1) * K)))) -m = -(-N//K) -print(abs(N - m*K)) +print(min(N%K, abs(N%K - K))) -print((min(N % K, abs(N % K - K))))