
var n, temp, max, divider: longint;
	x, qnt, qnt1, i: integer;
  	tf: boolean;

begin
	readln(n);
    while n <> 0 do begin
        temp := n;
        qnt := 1;
        while temp div 10 <> 0 do begin
            temp := temp div 10;
            inc(qnt);
        end;
        divider := 10;
        qnt1 := 1;
        max := 0;
        temp := n;
        while True do begin
            x := temp mod divider;
            tf := False;
            for i := 2 to trunc(sqrt(x)) do
                if x mod i = 0 then begin
                    tf := True;
                    break;
            end;
            if (tf = False) and (x >= max) then
                max := x;
            temp := temp div 10;
            if temp = 0 then begin
                inc(qnt1);
                temp := n;
                divider := divider * 10;
                if qnt1 = qnt + 1 then
                    break;
            end;		
        end;
        writeln(max);
        readln(n);
    end;
end.