#include <bits/stdc++.h>
int rev(int a) {
  int r = 0;
  while (a) {
    r = 10 * r + a % 10;
    a /= 10;
  }
  return r;
}
int prime[666666], cnt = 0;
int main() {
  bool a[999999];
  int i, j;
  int c = 999999, d = 0, k = 0, n, p;
  memset(a, 1, sizeof(a));
  for (i = 2; i < c; i++) {
    if (a[i]) prime[cnt++] = i;
    for (j = 0; j < cnt && i * prime[j] < c; j++) {
      a[i * prime[j]] = 0;
      if (i % prime[j] == 0) break;
    }
  }
  scanf("%d", &n);
  while (k < n) {
    p = rev(prime[d]);
    if (prime[d] != p && a[p]) k++;
    d++;
  }
  printf("%d", prime[d - 1]);
  return 0;
}
