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

int main(){
    int n,k;
    scanf("%d %d",&n, &k);

    int tmp = n%k;
    if(tmp <= abs(tmp-k))printf("%d\n",tmp);
    else printf("%d\n",abs(tmp-k));
    
    return 0;
}
