#include <stdio.h>

int main(void) {  
 int N=0, K=0;
  scanf("%d%d",&N,&K);

 N = N-int(N/K)*K;

  while (1){
    int dif =N-K;
    if (dif < 0) dif *= -1;

    if (N >= dif) N = dif;
    else break;

  }

  printf ("%d",N);
  
  return 0;
}