#include<stdio.h>
int main(void){
    long long x,k,ab;
    scanf("%lld %lld",&x,&k);
    x-=x/k*k;
    ab=x-k;
    if(ab<0){
        ab*=-1;
    }
    if(x<ab){
        printf("%lld\n",x);
    }else{
        printf("%lld\n",ab);
    }
    return 0;
}