def solve(string):

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

    n %= k

    while n > abs(n - k):

        n = abs(n - k)

    return str(n)





if __name__ == '__main__':

    import sys

    print((solve(sys.stdin.read().strip())))

def solve(string):

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

    n %= k

    return str(min(n, abs(n - k)))





if __name__ == '__main__':

    import sys

    print((solve(sys.stdin.read().strip())))

def solve(string):
    n, k = list(map(int, string.split()))
    n %= k
    while n > abs(n - k):
        n = abs(n - k)
    return str(n)


if __name__ == "__main__":
    import sys

    print((solve(sys.stdin.read().strip())))

def solve(string):
    n, k = list(map(int, string.split()))
    n %= k
    return str(min(n, abs(n - k)))


if __name__ == "__main__":
    import sys

    print((solve(sys.stdin.read().strip())))

-    while n > abs(n - k): -        n = abs(n - k) -    return str(n) +    return str(min(n, abs(n - k)))