import java.math.BigInteger;
import java.util.Scanner;

/**
 * Created by alex on 12.02.2016.
 */
public class prog {

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        long A, B;
        A = in.nextLong();
        B = in.nextLong();

        long sol = -1;

        for (int i = 0; i < 5000; ++i)
            for (int j = 0; j < 5000; ++j)
            {
                long C = A - i;
                long D = B - j;

                if (C > 0 && D > 0){

                    if (GCD(C, D) == 1)
                        sol = max(sol, abs(C - D) + 1);
                }
            }

        System.out.println(sol);
    }

    private static long max(long sol, long l) {

        if (sol > l)
            return sol;
        else
            return  l;
    }

    public static long abs(long x){

        if (x > 0)
            return  x;
        else
            return  -x;
    }

    public static long GCD(long a, long b){

        if (b == 0)
            return a;
        else
            return GCD(b, a % b);
    }
}