/*
 * FileName:     C_fix
 * CreatedDate:  2020-04-04 21:28:10 +0900
 * LastModified: 2020-04-04 21:34:30 +0900
 */

#include <stdio.h>
#include <stdlib.h>
long int min(int s, int t){
    if(s<t){return s;}
    else{ return t; }
}
int main(void){
    long int n,k;
    scanf("%ld %ld",&n,&k);
    long int tmp=n/k;
    printf("%ld\n", min(n-tmp*k,-n+(tmp+1)*k));
    return 0;
}
