#include<stdio.h>
int main()
{
    long long int k,n;
    scanf("%lli%lli",&n,&k);
    if(n<k){if(2*n>k){n=k-n;}}
    else if(n>=k){if(n%k<=k/2){n=n%k;}
    else if(n%k>k/2){n=k-(n%k);}}
    printf("%lli",n);
    return 0;
}
