n,k=list(map(int,input().split()))

n%=k

N=abs(n-k)

while N<n:

    n=N

    N=abs(n-k)

print(n)

n,k=list(map(int,input().split()))

n%=k

N=abs(n-k)

if N<n:

  n=N

print(n)

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

n, k = list(map(int, input().split()))
n %= k
N = abs(n - k)
if N < n:
    n = N
print(n)

-while N < n: +if N < n: -    N = abs(n - k)