#include <bits/stdc++.h>
int flag[1000010];
int prime[1000010];
int ans[100010];
int p[20];
int main() {
  int pcnt = 0;
  memset(flag, -1, sizeof(flag));
  for (int i = 2; i <= 1000000; i++) {
    if (flag[i]) prime[pcnt++] = i;
    for (int j = 0; j < pcnt && prime[j] <= 1000000 / i; j++) {
      flag[i * prime[j]] = 0;
      if (i % prime[j] == 0) break;
    }
  }
  int tcnt = 0;
  for (int i = 0; i < pcnt; i++) {
    int num = prime[i];
    int cnt = 0;
    while (num != 0) {
      p[cnt++] = num % 10;
      num /= 10;
    }
    int tnum = 0;
    for (int i = 0; i < cnt; i++) tnum = tnum * 10 + p[i];
    if (tnum != prime[i] && flag[tnum]) ans[tcnt++] = prime[i];
  }
  int n;
  while (scanf("%d", &n) == 1) {
    printf("%d\n", ans[n - 1]);
  }
}
