#include <stdio.h>
#include <stdlib.h>
 
int main(void){
    int n, k;
    scanf("%d %d", &n, &k);
    if (k == 1){
        printf("0\n");
    }else{
        while ((double)n >= (double)k / 2){
            n = abs(k - n);
        }
        printf("%d\n", n);
    }
    return 0;
}