#include <stdio.h>
int main(void)
{
  long int n,k;
  int t,a,min; 
  scanf("%ld",&n);
  scanf("%ld",&k);
  t=n%k;
  a=t-k;
  if(a<0)
    a=a*-1;
  if(t<a)
    min=t;
  else
    min=a;
  printf("%d",min);
}