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

int main(void) {
  unsigned long long n, k;
  scanf("%d %d", &n, &k);
  unsigned long long a = n % k;
  if(a > (unsigned long long)fabs(a-k)) {
    printf("%d\n", (unsigned long long)fabs(a-k));
  }
  else {
    printf("%d\n", a);
  }
  return 0;
}
