#include <assert.h>
#include <stdio.h>

long long int absll(long long int n) {
  if (n < 0) {
    return -n;
  }
  return n;
}

int main() {
  long long int n, k, l, m;
  assert(scanf("%lld %lld", &n, &k) == 2);
  l = absll(n - k * (long long int)(n / k));
  m = absll(n - k * (long long int)(n / k + 1));
  printf("%lld\n", l > m ? m : l);
}
