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

N=N%K

while abs(N-K)<N:

    N=abs(N-K)

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

N=N%K

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

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

-while abs(N - K) < N: -    N = abs(N - K) -print(N) +print((min(N, abs(N - K))))