#include <bits/stdc++.h>
static const int INF = std::numeric_limits<int>::max();
int main() {
  int a;
  std::cin >> a;
  std::vector<int> primes(1000001, 1);
  primes[0] = primes[1] = 0;
  for (int i = 0; i < (int)primes.size(); ++i) {
    if (primes[i] == 1) {
      for (int j = 2; i * j < (int)primes.size(); ++j) {
        primes[i * j] = 0;
      }
    }
  }
  for (int i = 0; i < (int)primes.size(); ++i) {
    if (primes[i] == 1) {
      std::stringstream ss;
      ss << i;
      std::string x;
      ss >> x;
      if (!std::equal(x.begin(), x.end(), x.rbegin())) {
        ss.clear();
        ss << std::string(x.rbegin(), x.rend());
        int y;
        ss >> y;
        if (primes[y]) {
          --a;
          if (a == 0) {
            std::cout << i << std::endl;
            return 0;
          }
        }
      }
    }
  }
  assert(0);
}
