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

int main(void){

    long long int N,K,i,ans;

    scanf("%lld %lld",&N,&K);

    if(N < K/2){
        //printf("judge1\n");
        ans = N;
    }else if(N < K){
        //printf("judeg2\n");
        ans = abs(N - K);
    }else{
        if(abs(N - (K * (N/K))) < abs(N - (K * ((N/K) + 1)))){
            //printf("judeg3\n");
            ans = abs(N - (K * (N/K)));
        }else{
            //printf("judeg4\n");
            ans = abs(N - (K * ((N/K) + 1)));
        }
    }

    printf("%lld\n",ans);
    return 0;
}
