#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> primes;
vector<string> ans;
int main() {
  cin >> N;
  primes.push_back(2);
  for (int i = 3; i < 1000000; i += 2) {
    bool pr_int = true;
    for (int p : primes) {
      if (i % p == 0) {
        pr_int = false;
        break;
      }
      if (p * p > i) break;
    }
    if (pr_int) primes.push_back(i);
  }
  for (int pr : primes) {
    string s;
    string r;
    s = to_string(pr);
    r = "";
    for (char c : s) {
      r = c + r;
    }
    if (s == r) continue;
    int prime = stoi(r);
    bool found = false;
    auto it = lower_bound(primes.begin(), primes.end(), prime);
    if (*it == prime) ans.push_back(s);
  }
  cout << ans[N - 1];
  return 0;
}
