#include <stdio.h>

int min(int a,int b){
  if(a <= b){return a;}
  else{return b;}
}
int main(void){
  int N,K,r;
  scanf("%d",&N);
  scanf("%d",&K);
  r = N%K;
  printf("%d",min(r,K-r));
  return 0;
}
