{$A+,B+,D+,E+,F+,G+,I+,L+,N+,O+,P+,Q-,R-,S-,T+,V+,X+,Y+}
{$M 16384,0,655360}
{
TASK:crazy
LANG:Pascal
}
uses module;
var a,b,i:longint;
    f:boolean;
    w:array[1..1000] of byte;
function isprime(a:longint):boolean;
var i,j:longint;
begin
if (a=1) or (a=2) or (a=3) then
   begin
   isprime:=true;
   exit;
   end;
if not odd(a) then
   begin
   isprime:=false;
   exit;
   end;
i:=3;
j:=round(sqrt(a));
while i<=j do
      begin
      if a mod i=0 then
         begin
         isprime:=false;
         exit;
         end;
      inc(i,2);
      end;
isprime:=true;
end;
function nod(a,b:longint):longint;
var c:longint;
begin
repeat
c:=a mod b;
a:=b;
b:=c;
until b=0;
nod:=a;
end;
function check(a,b:longint):boolean;
var i,j:longint;
begin
if w[a]=0 then for i:=2 to a-1 do if nod(a,i)<>1 then if not check(i,b+1) then
    begin
    check:=true;
    exit;
    end;
if w[b]=0 then for i:=2 to b-1 do if nod(b,i)<>1 then if not check(a+1,i) then
    begin
    check:=true;
    exit;
    end;
check:=false;
end;
begin
for i:=1 to 1000 do if isprime(i) then w[i]:=1 else w[i]:=0;
repeat
getnum(a,b);
f:=false;
if w[a+1]=1 then
   begin
   for i:=2 to b-1 do if nod(b,i)<>1 then if not check(a+1,i) then
       begin
       f:=true;
       break;
       end;
   if f then setnum(a+1,i);
   end;
if (not f) and (w[a]=0) then
   begin
   for i:=2 to a-1 do if nod(a,i)<>1 then if not check(i,b+1) then
       begin
       f:=true;
       break;
       end;
   if f then setnum(i,b+1);
   end;
if (w[b]=0) and (not f) then
   begin
   for i:=2 to b-1 do if nod(b,i)<>1 then if not check(a+1,i) then
       begin
       f:=true;
       break;
       end;
   if f then setnum(a+1,i);
   end;
until false;
end.