#include <bits/stdc++.h>
using namespace std;
int prime[200005];
int primes[200005], len;
void sieve(int n) {
  memset(prime, 1, sizeof(prime));
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i * i <= 200005; ++i) {
    if (prime[i]) {
      for (int k = 2; k * i <= 200005; ++k) {
        prime[k * i] = 0;
      }
    }
  }
  len = 0;
  for (int i = 12; i < 200005; ++i) {
    if (prime[i]) primes[len++] = i;
  }
}
string toStr(int n) {
  string s;
  ostringstream buffer;
  buffer << n;
  s = buffer.str();
  return s;
}
int toInt(string str) {
  int n;
  istringstream buffer(str);
  buffer >> n;
  return n;
}
bool isPrime(int n) {
  int i;
  if (n == 2) return 1;
  if (n % 2 == 0) return 0;
  for (i = 3; i * i <= n; i += 2)
    if (n % i == 0) return 0;
  return 1;
}
bool isEmirp(int n) {
  string s = toStr(n), aux = s;
  reverse(s.begin(), s.end());
  if (s == aux) return false;
  if (isPrime(toInt(s))) return true;
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 12;; ++i) {
    if (isPrime(i) && isEmirp(i)) {
      n--;
      if (n == 0) {
        printf("%d\n", i);
        return 0;
      }
    }
  }
  return 0;
}
