#include <stdio.h>

int main(void)
{
 int n,k;
  
  scanf("%d %d", &n,&k);
  
  if(n%k == 0){
    n = 0;
  }else{
    n = n%k;    
    if(n > k-n)
      n = k-n;
  }
  
  printf("%d", n);
  
  
  return 0;
  
}