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

int main(void){
  
  long int n,k,min;
  scanf("%ld %ld",&n,&k);
  
  while(1){
    
    min=n;

    n=labs(n-k);
    
    if(min<=n) break;
    
  }
  
  printf("%ld",min);
  
  return 0;
}