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