#include <stdio.h>

int main(void)
{
 long long int n,k,s,p;
  
  scanf("%lld %lld", &n, &k);
  
  s = n%k;
  p = k-s;
  
  if(s<p)
    printf("%lld", s);
  else
    printf("%lld", p);
    
  return 0;
  
}
