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

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