{
TASK:crazy
LANG:Pascal
}
program crazy;
uses module;

function prosto(n:integer):boolean;
var
	k:boolean;
   i:integer;
begin
k:=true;
i:=2;
while k and (i<=trunc(sqrt(n))) do
	begin
	if n mod i=0 then k:=false;
	i:=i+1;
   end;
prosto:=k;
end;

function fm(n,s:integer):integer;
var
	i:integer;
begin
i:=s;
while n mod i<>0 do
	i:=i+1;
fm:=i;
end;

function vp(a,b:integer):boolean;
var
	i:integer;
   k:boolean;
begin
k:=false;
i:=2;
while not k and (i<=trunc(sqrt(b))) do
	begin
   if (a mod i=0)and(b mod i=0) then k:=true;
   i:=i+1;
   end;
vp:=k;
end;

function solve(n:integer):integer;
var
	i:integer;
begin
i:=n-1;
while not vp(n,i) do
	i:=i-1;
solve:=i;
end;

var
	x,y,i,x1,y1:longint;

begin
WHILE (TRUE) DO
	BEGIN
		getnum(x,y);
      x1:=x;
		y1:=y;
      if prosto(y) then
      	if y=2 then
         	x1:=fm(x,2)
         else
         	x1:=fm(x,3)
      else
      	if prosto(x) then
         	if x=2 then
            	y1:=fm(y,2)
            else
            	y1:=fm(y,3)
      	else
				if prosto(x+1) then
            	y1:=fm(y,2)
            else
            	if prosto(y+1) then
               	x1:=fm(x,2)
               else
               	if prosto(x+2) then
                  	x1:=solve(x)
                  else
                  	if prosto(y+2) then
                     	y1:=solve(y)
                     else
								x1:=solve(x);
      if x1=x then x1:=x+1
      else
      	y1:=y+1;
      setnum(x1,y1);
	END;
end.