#include <iostream>

#define lint long long int
using namespace std;

lint gcd(lint a, lint b) {
    if (!b)
        return a;
    lint r = a % b;
    while(r) {
        a = b;
        b = r;
        r = a% b;
    }

    return b;
}

int main()
{
    lint a, b;
    cin >> a >> b;

    lint best = -2, c, d;
    for (int i = 0; i <= 2005; ++ i)
    for (int j = 0; j <= 2005; ++ j) {
        c = a + i;
        d = b - j;

        if (c <= d && gcd(c, d) == 1) {
            if (d - c > best)
                best = d - c;
        }
    }

    cout << best + 1 << '\n';
    return 0;
}