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

int main (void) {
  long int N, K;
  long int x, x1, xmin, befxmin;

  scanf("%d %d", &N, &K);
  x = N;
  xmin = N;
  befxmin = N;

  if (K != 1) {
    while (1) {
      x1 = fabs(x - K);
      if (x1 < x) {
	xmin = x1;
      }
      x = x1;
      if (xmin >= befxmin) {
	break;
      } else {
	befxmin = xmin;
      }
    }
    printf("%ld\n", xmin);
  } else {
    printf("0\n");
  }
  
  return 0;
}
