#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, k, bn, an;
    scanf("%d%d", &n, &k);
    if (n / k == 0)
        printf(0);
    else if (n <= k / 2)
        printf("%d",n);
    else
    {
        an = abs(n - k);
        do {
            bn = an;
            an -= abs(an - k);
        }while (an < bn);
        printf("%d",an);
    }
}