#include <bits/stdc++.h>
using namespace std;
int emirp[11184];
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
int main() {
  int next = 2;
  for (int i = 0; i < 11184; i++) {
    while (true) {
      while (!isPrime(next++))
        ;
      next--;
      stringstream ss;
      ss << next;
      string s = ss.str();
      reverse(s.begin(), s.end());
      stringstream ss2;
      int asdf;
      ss2 << s;
      ss2 >> asdf;
      if (asdf != next && isPrime(asdf)) {
        emirp[i] = next++;
        break;
      }
      next++;
    }
  }
  int n;
  cin >> n;
  cout << emirp[n - 1] << endl;
}
