#include<stdio.h>
int main(){
  long int a, b, c;
  scanf("%ld %ld", &a, &b);
  long int d = a % b;
  if(b % 2 == 1){
    if(d <= (b - 1) / 2){
      printf("%ld", d);
    }else{
      c = b - d;
      printf("%ld", c);
    }
  }
  else{
    if(d < b / 2){
      printf("%ld", d);
    }else{
      c = b - d;
      printf("%ld", c);
    }
  }
  
  return 0;
}
