#include <bits/stdc++.h>
using namespace std;
char s[1000010];
bool a[1000010];
int q[1000010];
int ff(int n) {
  int kkk = n;
  int i, k = 0, aa[9], SS = 0;
  while (n > 0) {
    aa[k++] = n % 10;
    n /= 10;
  }
  for (i = 0; i < k; i++) SS = SS * 10 + aa[i];
  if (SS == kkk) return 4;
  return SS;
}
int pri[1000010];
void f() {
  memset(a, false, sizeof(a));
  int i, j, now = 0;
  for (i = 2; i <= 1000010 - 3; i++) {
    if (!a[i]) {
      pri[now++] = i;
      for (j = 2 * i; j <= 1000010 - 3; j += i) a[j] = true;
    }
  }
  int now2 = 0;
  for (i = 0; i < now - 1; i++) {
    if (pri[i] < 12) continue;
    int k = ff(pri[i]);
    if (k > 1000010) continue;
    if (!a[k]) {
      q[++now2] = pri[i];
    }
  }
}
int main() {
  int n, m, i;
  f();
  while (~scanf("%d", &n)) {
    printf("%d\n", q[n]);
  }
  return 0;
}
