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

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

ans = min(N%K, abs(N%K-K))

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

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

-print((min(N % K, abs(N % K - K)))) +ans = min(N % K, abs(N % K - K)) +print(ans)