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

long long absolute(long long a,long long b)
{
  if(a > b)
    return a - b ;
  else
    return b - a ;
}

int main()
{
  long long N,K;
  int i,ans;
  long long *a;

  scanf("%lld %lld",&N,&K);

  a = calloc(N,sizeof(long long));

  a[0] = absolute(N,K);
  for(i=1 ;; i++)
  {
    a[i] = absolute(a[i-1],K);
    if(a[i] >= a[i-1])
      continue;
    else
      ans = i;
      break;
  }

  printf("%lld",a[ans] );
  return 0;
}
