#include <stdio.h>

int main(void){
  long N,K;
  scanf("%ld %ld",&N,&K);

  if(N < K){
    printf("%ld",N);
  }else{
    long z = N%K;
    long x = N - N%K;
    long y = (N/K + 1)*K - N;

    if(z < y && z < x){
      printf("%ld",z);
    }else if(x < y){
      printf("%ld",x);
    }else{
      printf("%ld",y);
    }
  }
}
