#include<stdio.h>
 
int main(void)
{
  long N, K, num1, num2;
  int flag = 0;
  
  scanf("%d %d", &N, &K);
  
  num1 = N % K;
  
  num2 = num1 - K;
  
  if(num2 < 0)
    num2 *= -1;
  
  if(num1 < num2)
    printf("%ld\n", num1);
  
  else
    printf("ld\n", num2);
  
  return 0;
}