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

int func(long n, long k);
int main(void);

int func(long n, long k){
    long old, new, n_tmp;
    long i=0;
    n_tmp = n;
    old = n;
    while(1){
        if(i==0){
            new = n%k;
        }else{
            old = new;
            new = labs(n_tmp-k);
        }
        if(new > old)
            break;
        n_tmp = n_tmp - k;
        i++;

    }

    return old;
}

int main(void){
    long n, k;
    int min;
    scanf("%d %d", &n, &k);
    min = func(n, k);

    printf("%d\n", min);

    return 0;
}
