#include <stdio.h>
int main(void){
  int N, K, a, b, x;
    scanf("%d", &N); scanf("%d", &K);
  x = K / 2;
  if(x > N) printf("%d", N);
  else{
    a = N % K;
    b = a - K; 
  	if(b < 0) b = b * -1;
    if (a < b) printf("%d", a);
    else printf("%d", b);
    return 0;
  };
}