#include <stdio.h>


int main()
{
    long a, b,c;
    scanf("%ld %ld", &a, &b);
    if(b==1){
        a = 1;
    }
        while (1)
        {
            if (a > b)
            {
                c= a - b;
            }
            else{
                c= b - a;
                if(c>=a)
                    break;
            }
                a = c;
        }

    if(a==b)
        a = 0;
    printf("%ld\n", a);
}