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