#include <bits/stdc++.h>
using namespace std;
bool prime(long long num) {
  if (num == 2 || num == 3 || num == 5) return true;
  unsigned long c = 7;
  if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0 || num == 1) return false;
  int maxc = int(sqrt(num));
  while (c <= maxc) {
    if (num % c == 0) return false;
    c += 4;
    if (num % c == 0) return false;
    c += 2;
    if (num % c == 0) return false;
    c += 4;
    if (num % c == 0) return false;
    c += 2;
    if (num % c == 0) return false;
    c += 4;
    if (num % c == 0) return false;
    c += 6;
    if (num % c == 0) return false;
    c += 2;
    if (num % c == 0) return false;
    c += 6;
  }
  return true;
}
int main() {
  int b, a, i;
  cin >> a;
  int nnn = 0;
  int c;
  for (i = 12;; i++) {
    if (!prime(i)) continue;
    b = i;
    c = 0;
    while (b != 0) {
      c = c * 10 + b % 10;
      b = b / 10;
    }
    if (c != i && prime(c)) {
      nnn++;
    }
    if (nnn == a) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
