#include <stdio.h>
#include <stdlib.h>

int main(void){
  long n,k,tmp,p;
  scanf("%ld %ld", &n, &k);
  
  p=(int)(n/(double)k);
  n=n-(p*k);
  
  tmp=n-k;
  k=labs(tmp);
  
  if(n>=k){
    printf("%ld", n);
  }else{
    printf("%ld", k);
  }
  return 0;
}