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



# if N == K:

#     print(0)

#     exit(0)

# elif N < K:

#     print(N)

#     exit(0)



ans = []

before = abs(N - K)

N = N % K

for i in range(10000):

    ze = abs(N - K)

    ans.append(ze)

    before = ze

    N = ze

print((min(ans)))

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





N = N % K

ans1 = abs(N - K)

ans2 = abs(ans1-K)

print((min(ans1, ans2)))

N, K = list(map(int, input().split()))
# if N == K:
#     print(0)
#     exit(0)
# elif N < K:
#     print(N)
#     exit(0)
ans = []
before = abs(N - K)
N = N % K
for i in range(10000):
    ze = abs(N - K)
    ans.append(ze)
    before = ze
    N = ze
print((min(ans)))

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

-# if N == K: -#     print(0) -#     exit(0) -# elif N < K: -#     print(N) -#     exit(0) -ans = [] -before = abs(N - K) -for i in range(10000): -    ze = abs(N - K) -    ans.append(ze) -    before = ze -    N = ze -print((min(ans))) +ans1 = abs(N - K) +ans2 = abs(ans1 - K) +print((min(ans1, ans2)))