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

long long solve(long long n,long long k){
  long long r,t;

  if(k == 0) return n;

  r = n % k;

  if(r - k > 0) t = r - k;

  else t = k - r;

  if(r > t) return t;

  else return r;
}

int main(){
  long long n,k;

  scanf("%lld %lld", &n, &k);

  printf("%lld", solve(n,k));

  return 0;
}
