#include <stdio.h>
#include <limits.h>
 
int main()
{
  long int nn, kk, min = LONG_MAX;
  scanf("%ld %ld", &nn, &kk);
  long int old_nn = nn;
  if(nn > kk)
    nn = kk + (nn % kk);
  while(nn < min)
  {
    old_nn = nn;
    min = nn;
    if(nn > kk)
      nn = nn -kk;
    else if(kk > nn)
      nn = kk -nn;
    else
    {
      old_nn = 0;
      break;
    }
  }
  printf("%ld\n", old_nn);
  return 0;
}