#include<stdio.h>
int main(){
  int unused __attribute__((unused));
  long long N, K, t;
  unused = scanf("%lld %lld", &N, &K);

  t = N%K;
  if(t < (K-t)) printf("%lld", t);
  else printf("%lld", K-t);

  return 0;
}