#include <bits/stdc++.h>
int a[10000000];
int main() {
  int n, i, j, num[20000], h, i1;
  memset(a, 0, sizeof(a));
  for (i = 2; i <= 1000000; i++)
    if (a[i] == 0)
      for (j = i + i; j <= 1000000; j += i) a[j] = 1;
  h = 0;
  for (i = 10; i <= 1000000; i++) {
    n = i;
    i1 = 0;
    while (n != 0) {
      i1 = i1 * 10 + n % 10;
      n /= 10;
    }
    if (a[i] == 0 && a[i1] == 0 && i != i1) {
      h++;
      num[h] = i;
    }
  }
  scanf("%d", &n);
  printf("%d", num[n]);
}
