#include<stdio.h>

int main(){
  int N, K;
  int y, x;
  scanf("%d %d",&N,&K);
  x = N % K;
  y = x - K > 0 ? K -x : x -K;
  if(x < y ) printf("%d\n",x);
  else if(x == 0)  printf("0\n");
  else printf("%d\n",y);
}
