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

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

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

    }

    return old;
}

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

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

    return 0;
}
