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



def solve():

    global n, k

    n = n - (k * (n // k))

    pre = n

    while True:

        n = min(n, abs(n-k))

        if n == pre:

            break

        pre = n

    return n



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



n = n % k

print((min(abs(n - k), n)))
n, k = list(map(int, input().split()))


def solve():
    global n, k
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n - k))
        if n == pre:
            break
        pre = n
    return n


print((solve()))

n, k = list(map(int, input().split()))
n = n % k
print((min(abs(n - k), n)))

- - -def solve(): -    global n, k -    n = n - (k * (n // k)) -    pre = n -    while True: -        n = min(n, abs(n - k)) -        if n == pre: -            break -        pre = n -    return n - - -print((solve())) +n = n % k +print((min(abs(n - k), n)))