import java.util.Scanner;

/**
 * 双素数
 * @Author zhuyq
 * @Date 2021-09-17
 */
public class Main {
    private static int maxVal = 1000000;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println(getMinKDoublePrime(Integer.parseInt(in.nextLine())));
    }



    //输出第k小的双素数
    public static int getMinKDoublePrime(int k){
        int minKDoublePrime = -1;
        int count = 0;
        for(int i=1; i<=maxVal; i++){
            if(isDoublePrime(i)){
                count++;
                if(count==k){
                    minKDoublePrime=i;
                    break;
                }
            }
        }
        if(count<k){
            minKDoublePrime=-1;
        }
        return minKDoublePrime;
    }


    //判断是否是双素数
    public static boolean isDoublePrime(int number){
        boolean isDoublePrime = true;
        if(isPrime(number)){
            int reverseNumber = Integer.parseInt(new StringBuffer(String.valueOf(number)).reverse().toString());
            if(number!=reverseNumber && isPrime(reverseNumber)){
                isDoublePrime=true;
            }
            else{
                isDoublePrime=false;
            }
        }
        else{
            isDoublePrime=false;
        }
        return isDoublePrime;
    }



    //判断是否是素数
    public static boolean isPrime(int data){
        if(data<2) return false;
        for(int i=2;i<=Math.sqrt(data);i++){
            if(data%i==0) return false;
        }
        return true;
    }



}