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

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

  do
  {
    if(N >= K)
    {
      tmp = N - K;
    }
    else
    {
      tmp = K - N;
    }

    if(N >  tmp)
    {
      N = tmp;
    }
    else
    {
      break;
    }
  } while(N > 0);

  printf("%ld\n",N);

  return 0;
}