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

int main(void) {
        long long int n, k, tmp;
        scanf("%lld %lld",&n,&k);
        if( (n = n%k) > tmp = llabs(n -k)) printf("%lld\n",tmp);
        else printf("%lld\n",n);
        return 0;
}