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

long long abs_ll (long long num) {
    if ( num < 0 )
        return num * -1;
    return num;
}

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

    scanf("%lld%lld", &n, &k);

    long long ans = n % k;
    if ( ans > abs_ll(ans - k) )
        ans = abs_ll(ans - k);

    printf("%lld\n", ans);
    return EXIT_SUCCESS;
}
