#include <bits/stdc++.h>
using namespace std;
bool P[1000000];
vector<int> emirp;
long long int reverse(long long int x) {
  long long int rev = 0, dig;
  while (x > 0) {
    dig = x % 10;
    x /= 10;
    rev *= 10;
    rev += dig;
  }
  return rev;
}
void sieve() {
  long long i, j, rev;
  for (i = 2; i < 1000000; i++) {
    if (P[i]) continue;
    for (j = i * i; j < 1000000; j += i) P[j] = true;
  }
  for (i = 2; i < 1000000; i++) {
    if (!P[i]) {
      rev = reverse(i);
      if (!P[rev] && rev != i) emirp.push_back(i);
      if (emirp.size() >= 11184) break;
    }
  }
}
int main() {
  int n;
  sieve();
  cin >> n;
  cout << emirp[n - 1];
  return 0;
}
