#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef long long ll;

int main()
{
    ll n,k,i,j;
    scanf("%lld %lld",&n,&k);
    
    i=n%k;
    j=k-i;

    if(i>=j)
    {
        printf("%lld\n",j);
    }
    else
    {
        printf("%lld\n",i);
    }

}
