import java.util.*;
import java.io.*;

public class beauty_af implements Runnable {
	public static void main(String[] args) {
		new Thread(new beauty_af()).start();
	}
	
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof;
	
	public void run() {
		try {
			Locale.setDefault(Locale.US);
			br = new BufferedReader(new FileReader(FNAME + ".in"));
			out = new PrintWriter(FNAME + ".out");
			solve();
		} catch (IOException e) {
		} finally {
			out.close();
		}
	}
	
	String nextToken() {
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch(IOException e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}
	
	int nextInt() {
		return Integer.parseInt(nextToken());
	}
	
	long nextLong() {
		return Long.parseLong(nextToken());
	}
	
	double parseDouble() {
		return Double.parseDouble(nextToken());
	}
	
	String FNAME = "beauty";
	
	void myAssert(boolean e, String msg) { 
		if (!e) {
			throw new Error(msg);
		}
	}
	
	public void solve() throws IOException {
		int n = nextInt();
		boolean[] prime = new boolean[2000];
		Arrays.fill(prime, true);
		prime[0] = prime[1] = false;
		for (int i = 2; i < prime.length; i++) {
			if (prime[i]) {
				int j = 2 * i;
				while (j < prime.length) {
					prime[j] = false;
					j += i;
				}
			}
		}
		int c = 0;
		int i = 0;
		while (c < n) {
			int j = i;
			int k = 0;
			while (j > 0) {
				k += (j % 10) * (j % 10);
				j /= 10;
			}
			if (prime[k]) c++;
			i++;
		}
		out.println(i - 1);
	}
	
}
