#include <stdio.h>
int main(){
  long n, k = -1;
  int ret = scanf("%ld %ld", &n, &k);
  if(k == 1){
    n = 0;
  }else{
    n %= k;
    while(n > labs(n - k))
      n = labs(n - k);
  }
  printf("%ld", n);
  return 0;
}