#include <bits/stdc++.h>

using namespace std;

long long n, m, maxx = 0, razl = 0, fst, sec;

long long nzd(long long x, long long y)
{
    if (x < y) return nzd(y, x);
    if (x % y == 0) return y;
    return nzd(y, x % y);
}

int main()
{

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

    if (n == m && n != 1)
        maxx = -1;
    else
    {
        fst = n;
        sec = m;
        while (fst <= sec && (sec - fst + 1 > maxx)) // se sec ne spusti previse, a moguce je naci vecu razliku
        {
            bool nadjen = false;
            while (!nadjen && (sec - fst + 1 > maxx)) // dok se ne nadje prvi najdalji sec - u uzajamno prost , a moguce je naci vecu razliku
            {
                if (nzd(fst, sec) == 1) // uzajamno su prosti
                {
                    nadjen = true;
                    razl = sec - fst + 1;
                    if (razl > maxx)
                        maxx = razl;
                }
                fst++;
            }
            fst = n;
            sec--;
        }
    }

    printf("%lld\n", maxx);
	return 0;
}