#include <bits/stdc++.h>
using namespace std;
const double eps = 1.0e-11;
const double pi = acos(-1.0);
bool isPrime(long long a) {
  for (long long i = 2; i * i <= a; ++i) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
long long inv(long long a) {
  long long res = 0;
  while (a) {
    res += (a % 10);
    a /= 10;
    res *= 10;
  }
  return res / 10;
}
int main() {
  int n;
  cin >> n;
  int start = 13;
  int cnt = 1;
  while (true) {
    if (cnt == n) {
      cout << start << endl;
      return 0;
    }
    ++start;
    if (isPrime(start) && isPrime(inv(start)) && start != inv(start)) {
      ++cnt;
    }
  }
}
