#include<stdio.h>

int main(void){
    long int n,k,num;
    scanf("%ld%ld",&n,&k);
    if(n%k==0){
        printf("0\n");
        return 0;
    }
    n -= (n/k)*k;
    num = ((n/k)+1)*k-n;
    if(n<=num){
        printf("%ld\n",n);
    }else{
        printf("%ld\n",num);
    }
    return 0;
}
