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

int main(void) {
    long long start;
    long long sub_value;

    scanf("%lld %lld", &start, &sub_value);
//    printf("%lld\n", start);

    if (start < (llabs(start - sub_value))) {
        printf("%lld\n", start);
        return 0;
    }

    long long shurt_cut = (start / sub_value);
//    printf("%lld\n", shurt_cut);
    start = start / shurt_cut;
//    printf("%lld\n", start);
    long long result = start;
    long long old_result;
    while (result >= 0) {
        result = llabs(result - sub_value);

        // printf("%lld, %lld, %lld\n", result, start, sub_value);
        if (old_result <= result || result == 0) {
            if (start < result) {
                printf("%lld\n", start);
            } else {
                printf("%lld\n", result);
            }
            return 0;
        }
        old_result = result;
    }
/*
    if (result > 0) {
        printf("%d\n", result);
    } else {
        int result1 = 0 - result;
        int result2 = result + sub_value;
        if (result1 < result2) {
            printf("%d\n", result1);
        } else {
            printf("%d\n", result2);
        }
    }
*/
    return 0;
}
