#include<stdio.h>
#include<stdlib.h>
int main()
{
    unsigned long long int N,K,x;
    long long int y;

    scanf("%llu%llu",&N,&K);
    x=N;
    if(N>0 && K==1)
        printf("0\n");
    else
    {
        for(;;)
        {
            y=N-K;
            N=abs(N-K);
            if(y<=0)
                if(N<=x && N<K)
                    break;
        }
        printf("%d\n",N);


    }
    return 0;
}
