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

int main (void)
{
  unsigned long N, K;
  unsigned long diff_prev, diff_next;
  
  scanf("%lu %lu", &N, &K);
  
  diff_prev = abs(N-K);
  diff_next = abs(diff_prev-K);
  
  while (diff_next < diff_prev) {
    diff_prev = diff_next;
    diff_next = abs(diff_prev-K);
  }
   
  printf("%lu", diff_prev);
  
  return 0;
}

  
  