#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    long long int n,k,a;
    scanf("%lld %lld",&n,&k);
    if(n==k)
    printf("0\n");
    else if(n<k)
    {
        if(n<(k-n))
        printf("%lld\n",n);
        else
        printf("%lld\n",(k-n));
    }
    else
    {
        a=n%k;
        if(a<(k-a))
        printf("%lld\n",a);
        else
        printf("%lld\n",(k-a));
    }
}