{$apptype console}
var
  n : integer;

function isPrime(x : integer) : boolean;
var
  i : integer;
begin
  result := true;
  for i := 2 to x - 1 do begin
    if (x mod i = 0) then begin
      result := false;
      break;
    end;
  end;
  if (x = 1) then begin
    result := false;
  end;
end;

function digSqSum(x : integer) : integer; 
begin
  result := 0;
  while (x > 0) do begin
    result := result + (x mod 10) * (x mod 10);
    x := x div 10;
  end;
end;

var
  i : integer;
begin
  reset(input, 'beauty.in');
  rewrite(output, 'beauty.out');
  read(n);
  i := 1;  
  while (true) do begin
    if (isPrime(digSqSum(i))) then begin
      dec(n);
    end;
    if (n = 0) then begin
      writeln(i);
      break;
    end;
    inc(i);
  end;
end.