#include <stdio.h>
int main(void){
  int n,k;
  scanf("%d %d",&n,&k);
  while((n > n-k && n-k > 0)||(n > -n+k && -n+k < 0)){
    n = n-k;
    if(n < 0 )
      n = -n;
  }
  printf("%d",n);
  
  return 0;
}