#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
long long int min(long long int a,long long int b){

    if(a>=b){return b;}
    return a;
}

int main(void){
long long int n,k;
scanf("%lld %lld",&n,&k);
printf("%d\n",abs(min(abs(n%k),abs(n%k-k))));
}
