#include <bits/stdc++.h>
using namespace std;
bool primo[1000000];
int P[1000000], p;
int P2[1000000], p2;
int inv(int n) {
  int r = 0;
  while (n) {
    int d = n % 10;
    r = 10 * r + d;
    n /= 10;
  }
  return r;
}
int main() {
  p = 0;
  P[p++] = 2;
  memset(primo, true, sizeof(primo));
  for (int i = 3; i < 1000000; i += 2)
    if (primo[i]) {
      P[p++] = i;
      for (long long int j = (long long int)i * i; j < 1000000; j += i)
        primo[j] = false;
    }
  for (int i = 4; i < 1000000; i += 2) primo[i] = false;
  p2 = 0;
  for (int i = 0; i < p; i++)
    if (inv(P[i]) != P[i] and primo[inv(P[i])]) P2[p2++] = P[i];
  while (scanf("%d", &p) == 1) printf("%d\n", P2[p - 1]);
  return 0;
}
