#include <stdio.h>

int main(void)
{
    long long int inputN, inputK, answer = 0;
    scanf("%lld%lld", &inputN, &inputK);
    answer = inputN % inputK;
    if (answer > inputK - answer)
    {
        answer = inputK - answer;
    }
    printf("%lld", answer);

    return 0;
}