N,K  = (int(X) for X in input().split())

if N%K > abs(N%K-K):

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

else:

    print((N%K))
N,K  = (int(X) for X in input().split())

print((min(N%K,abs(N%K-K))))
N, K = (int(X) for X in input().split())
if N % K > abs(N % K - K):
    print((abs(N % K - K)))
else:
    print((N % K))

N, K = (int(X) for X in input().split())
print((min(N % K, abs(N % K - K))))

-if N % K > abs(N % K - K): -    print((abs(N % K - K))) -else: -    print((N % K)) +print((min(N % K, abs(N % K - K))))