#include <stdio.h>

int main()
{
  long x,k,p_min;
  long min,p,now;
  scanf("%ld",&x);
  scanf("%ld",&k);
  
  if(x>k){
    p = 0;
  }
  else{
    p = 1;
  }
  min = x;
  
  if(p==0){
    while(1){
      p_min = min;
      now = x - k;
      min = now;
      x = now;
      if(now<=0){
        min *= -1;
        break;
      }
      if(p_min==min)
        break;
    }
  }
  else if(p==1){
    now = k - x;
    if(min>=now)
      min = now;
  }

  if(p==0){
  	if(p_min < min)
      min = p_min;
  }
  
  printf("%ld",min);

  return 0;
}