#include <stdio.h>

#define N 100

int main(void) {
    unsigned int n, k;
    scanf("%d %d", &n, &k);
    unsigned int ans;

    n = n % k;
    ans = (n <= k-n) ? n : k-n;

    printf("%d\n", ans);

    return 0;
}