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

r = N%K

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

n = N%K

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

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

-r = N % K -print((min(r, K - r))) +n = N % K +print((min(n, K - n)))