#include <bits/stdc++.h>
int shai[1000001], prime[1000001], ans[1000001];
int main() {
  int plen = 0, i, j, n = 0;
  for (i = 2; i <= 1000000; ++i) {
    if (!shai[i]) prime[plen++] = i;
    for (j = 0; j < plen && prime[j] * i <= 1000000; ++j) {
      shai[i * prime[j]] = 1;
      if (i % prime[j] == 0) break;
    }
  }
  for (i = 1; i <= 1000000; ++i)
    if (!shai[i]) {
      int i2 = i, fan = 0;
      for (; i2; i2 /= 10) fan = fan * 10 + i2 % 10;
      if (fan != i && !shai[fan]) ans[++n] = i;
    }
  scanf("%d", &n);
  printf("%d\n", ans[n]);
  return 0;
}
