#include <bits/stdc++.h>
using namespace std;
int primes[35000] = {2, 3};
int len = 2;
bool isPrime(int i) {
  int j = 0;
  for (; (j < len) && (primes[j] * primes[j] <= i); ++j) {
    if (i % primes[j] == 0) return false;
  }
  return true;
}
int revert(int n) {
  int rev = 0;
  while (n != 0) {
    rev = rev * 10 + n % 10;
    n /= 10;
  }
  return rev;
}
int main() {
  for (int i = 3; i < 35000; ++i) {
    int j = 0;
    for (; (j < len) && (primes[j] * primes[j] <= i); ++j) {
      if (i % primes[j] == 0) break;
    }
    if ((j == len) || (primes[j] * primes[j] > i)) {
      primes[len++] = i;
    }
  }
  int founded = 0;
  int n;
  cin >> n;
  int c = 13;
  while (true) {
    int rev = revert(c);
    if (c == rev) {
      c++;
      continue;
    }
    if (isPrime(c) && isPrime(revert(c))) {
      founded++;
      if (founded == n) {
        cout << c << endl;
        break;
      }
    }
    ++c;
  }
  return 0;
}
