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

int main(void)
{
  char buf[1000];
  long n, k, now, before;

  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%ld %ld\n", &n, &k);

  before = n;

  while (1) {
    if (k == 1) {
      before = 0;
      break;
    }

    now = labs(n - k);

    if (now < before) {
      before = now;
      n = now;
    } else {
      break;
    }
  }

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

}