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

int main(void) {
        long long int n, k;
        scanf("%lld %lld",&n,&k);

        long long int tmp;
        while (1){
                tmp = n;
                n = abs(n-k);
                if (n >= tmp) {
                        n = tmp;
                        break;
                }
        }

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