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

int main (int argc, char *argv[]) {
    long long int n, k, menor;

    scanf("%lld %lld", &n, &k);

    menor = n;
    if (k < n) {
        n = k;
        k = menor;
    }

    if (n == 1) menor = 0;

    while (abs(n) > abs(n - k)) {
        n = abs(n - k);
    }

    if (menor == 0) {
        n = 0;
    }



    printf("%lld", n);

    return 0;
}