#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void){
    long int n, k;
    
    scanf("%ld%ld", &n, &k);
    
    long int min = n;
    
    if(k > n){
        printf("%ld", n);
        exit(0);
    }
    
    for(;;){
        n = abs(n - k);
        if(min > n){
            min = n;
        }else if(min == n){
            break;
        }
    }
    printf("%ld", min);
    
}
