#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
long long st, dr, sol = -1LL;

inline long long Euclid(long long a, long long b)
{
	long long r;
	while(b)
	{
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}

int main()
{
	long long i, j, x;
	cin >> st >> dr;
	x = st;
	while(x <= dr && Euclid(x, dr) > 1LL)
		x++;
	if(x <= dr)
		sol = max(sol, dr - x + 1LL);
	x = dr;
	while(x >= st && Euclid(st, x) > 1LL)
		x--;
	if(x >= st)
		sol = max(sol, x - st + 1LL);
	if(sol >= 0LL)
	{
		for(i = st; i + sol <= dr; ++i)
		{
			for(j = i + sol; j <= dr; ++j)
			{
				if(Euclid(i, j) == 1LL)
				{
					sol = max(sol, j - i + 1LL);
					j = max(j, i + sol - 1);
				}
			}
		}
	}
	cout << sol << "\n";
	return 0;
}