#include<stdio.h>
 
int main(void){
  int N,K=0;
  
  scanf("%d",&N);
  scanf("%d",&K);

if(N >= K){

 while(2*N < K && 0 < K){

 N = N - K;

 }

}

else{

 while(K < 2*N){

 N = K - N;

 }

}

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