int main(void){
  long int n,k,i;
  
  scanf("%ld %ld",&n,&k);
  
  if(n <= k){
    printf("%ld",n);
    return 0;
  }
    
  if(n%k < (n%k-k) * -1)
    printf("%ld",n%k);
  else
    printf("%ld",(n%k-k) * -1);
}