
#include <stdio.h>

int main(void)
{
    long int n=0,k=0;
    long int extra=0,ans=0;

    scanf("%ld %ld",&n,&k);
    extra=n%k;
    if(extra>k-extra){
        ans=k-extra;
    } else {
        ans=extra;
    }

    printf("%ld",ans);

    return 0;
}
