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

print((min(abs(N - (K*(N//K+1))), abs(N - (K*(N//K))))))
N, K = list(map(int, input().split()))

A = abs(N - (K*(N//K+1)))

B = abs(N - (K*(N//K)))

print((min(A, B)))

N, K = list(map(int, input().split()))
print((min(abs(N - (K * (N // K + 1))), abs(N - (K * (N // K))))))

N, K = list(map(int, input().split()))
A = abs(N - (K * (N // K + 1)))
B = abs(N - (K * (N // K)))
print((min(A, B)))

-print((min(abs(N - (K * (N // K + 1))), abs(N - (K * (N // K)))))) +A = abs(N - (K * (N // K + 1))) +B = abs(N - (K * (N // K))) +print((min(A, B)))