#include <bits/stdc++.h>
using namespace std;
int cmpint(const void *a, const void *b) {
  if (((int *)a)[0] == ((int *)b)[0]) return ((int *)a)[1] - ((int *)b)[1];
  return ((int *)a)[0] - ((int *)b)[0];
}
int prim[500005] = {0};
bool isprim[5000005] = {0};
int prim_len;
void count_prim() {
  int i, j, l;
  prim[0] = 2;
  prim_len = 1;
  for (i = 3; i < 5000000; i += 2)
    if (isprim[i] == 0) {
      prim[prim_len] = i;
      prim_len++;
      l = i * 2;
      if (i < 10000)
        for (j = i * i; j < 5000000; j += l) isprim[j] = 1;
    }
}
int a[100005];
int main() {
  int i, j, n, m, k, l, o, p;
  scanf("%d", &n);
  count_prim();
  m = 0;
  for (i = (0); i < (prim_len); i++) {
    k = prim[i];
    l = 0;
    while (k > 0) {
      l = l * 10 + k % 10;
      k /= 10;
    }
    if (l != prim[i] && isprim[l] == 0 && l % 2 == 1) {
      m++;
      a[m] = prim[i];
    }
    if (m > n) break;
  }
  printf("%d\n", a[n]);
  return 0;
}
