#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  const int MAX = 1e6;
  int SQRT_MAX = 1e3;
  bitset<MAX + 1> sieve;
  sieve.set(0);
  sieve.set(1);
  for (int i = 4; i <= MAX; i += 2) sieve.set(i);
  for (int i = 3, j; i <= SQRT_MAX;) {
    for (j = i * i; j <= MAX; j += (i << 1)) sieve.set(j);
    while ((i += 2) <= SQRT_MAX && sieve[i])
      ;
  }
  sieve.flip();
  int idx = 0;
  for (int i = 2;; ++i) {
    if (!sieve.test(i)) continue;
    stringstream ss;
    ss << i;
    string str;
    ss >> str;
    ss.clear();
    reverse(str.begin(), str.end());
    ss << str;
    int rev;
    ss >> rev;
    if (i == rev || !sieve.test(rev)) continue;
    ++idx;
    if (idx == n) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
