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