#include <stdio.h>
#include <stdlib.h>

int main(void){

    int N = 0;
    int K = 0;
    int tmp = 0;
    int flag = 0;
    scanf("%d %d", &N, &K);

    N = N % K;

    while (flag == 0){
        tmp = N;
        N = abs(N - K);
        if (tmp <= N){
            N = tmp;
            flag = 1;
        }
    }

    printf("%d", N);
    return 0;
}