#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
  long int N,K,tmp,a1,a2;
  scanf("%ld %ld",&N,&K);

  tmp = N/K;
  a1 = N - tmp*K;
  a2 = K - a1;

  if(a1 > a2)
  {
    printf("%ld\n",a2);
  }
  else
  {
    printf("%ld\n",a1);
  }

  return 0;
}