#include <bits/stdc++.h>
using namespace std;
bool isPrime(int a) {
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) return false;
  }
  return true;
}
int reverse(int a) {
  int res = 0;
  while (a != 0) {
    res = res * 10 + a % 10;
    a /= 10;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  int count = 0;
  for (int i = 2;; i++) {
    if (isPrime(i)) {
      int t = reverse(i);
      if (isPrime(t) && t != i) {
        count++;
        if (n == count) {
          cout << i;
          return 0;
        }
      }
    }
  }
  return 0;
}
