#include<stdio.h>
 int main(void)
 {
     int N,K,s,t;
     scanf("%d%d",&N,&K);
     if(N%K==0) printf("0\n");
   
     while(N>K)
      N=N-K;

     if(N<K) s=K-N;

     if(N<s) printf("%d\n",N);
     else    printf("%d\n",t);
   
     return 0;
 }