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

public class beauty_vu {

	static int sqr(int a) {
		return a * a;
	}

	static boolean good(int k) {
		int l = 0;
		while (k > 0) {
			l += sqr(k % 10);
			k /= 10;
		}
		if (l == 1) return false;
		for (int i = 2; i < l; i++) {
			if (l % i == 0) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(new File("beauty.in"));
		PrintWriter out = new PrintWriter(new File("beauty.out"));
		
		int n = in.nextInt();
		int l = 0;
        int k = 1;
        while (l < n) {
        	k++;
        	if (good(k)) l++;
        }

		out.println(k);	

		in.close();
		out.close();
	}

}