int main(){
  long int a,b;
  scanf("%ld%ld",&a,&b);
  if(a>b){
    a%=b;
    if((int)a==0){
      printf("%d",0);
    }else{
      b%=a;
      if(a<b) printf("%ld",a);
      else printf("%ld",b);
    }
  }else{
    b-=a;
    if(b<a) printf("%ld",b);
    else printf("%ld",a);
  }
  return 0;
}