#include <stdio.h>

int main(void)

{

    long n, k;

    long m;

    scanf("%ld %ld",&n, &k);

    n = n % k;

    if (n < 0)

        n *= -1;

    m = n;

    if (n < k - n)

        printf("%ld", n);

    else

        printf("%ld", k - n);

    return (0);

}