#include<stdio.h>
#define min(a,b) ((a)<(b)?(a):(b))

int main(void){
    unsigned long long x,k;
    scanf("%llu %llu",&x,&k);
    if(x < k){
        printf("%llu",min(k-x,x));
        return 0;
    }if(x == k){
        puts("0");
        return 0;
    }
    printf("%llu",min(x % k,k - (x % k)));
    return 0;
}
