import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;

T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}

//END OF TEMPLATE

void main(){
    ulong n,k;
    scan(n,k);
    ulong x;
    if(n<k)
        x=min(n,k-n);
    else if(k==n)
        x=0;
    else{
        x=min(n%k,abs((n%k).to!long-k.to!long));
    }   
    x.writeln;
}
