#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool isPrime(long long p) {
  if (p < 2) return 0;
  for (long long i = 2; i * i <= p; i++)
    if (p % i == 0) return 0;
  return 1;
}
long long reverse(long long p) {
  long long q = 0;
  while (p != 0) {
    q *= 10;
    q += p % 10;
    p /= 10;
  }
  return q;
}
int main() {
  long long n, r, ct = 0;
  cin >> n;
  for (long long i = 1;; i++) {
    if (isPrime(i)) {
      r = reverse(i);
      if (r != i && isPrime(r)) {
        ct++;
        if (ct == n) {
          cout << i << endl;
          return 0;
        }
      }
    }
  }
}
