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