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