#include <stdio.h>
#include <math.h>

int min(int x, int y){
    if(x < y){
        return x;
    }else{
        return y;
    }
}

int main(int argc, char const *argv[])
{
    int n,k;
    scanf("%d %d",&n,&k);

    if(k == 1){
        printf("%d",0);
    }else if(n % k == 0 && n > k){
        printf("%d",0);
    }else if(n == k){
        printf("%d",0);
    }else{
        int i,minx,premin,tmp;
        tmp = n;
        minx = n;
        premin = n;

        for(i=0;i < 100;i++){
            tmp = fabs(tmp - k);
            minx = min(minx,tmp);
            if(minx == 0 || premin == tmp){
                printf("%d",tmp);
                return 0;
            }else{
                premin = minx;
            }
        }
    }
    return 0;
}