#include <stdio.h>
#include <stdlib.h>
 
int main()
{
int x,k,n,tmp;
 
scanf("%d%d",&n,&k);

if(n<k){
    tmp=k;
    k=n;
    n=tmp;
}
tmp=n%k;
if(abs(k-tmp)<tmp){tmp=abs(k-tmp);}

printf("%d\n",tmp);

return 0;
}