#include <bits/stdc++.h>
int a[1000001], c[12000];
void prime() {
  int n, m, i, j, k = 1, s;
  memset(a, 0, sizeof(a));
  a[0] = a[1] = 1;
  for (i = 0; i <= 1000001; i++) {
    if (a[i] == 0) {
      for (j = i + i; j <= 1000001; j += i) a[j] = 1;
    }
  }
}
int rev(int n) {
  int i, j, k, t = 0;
  for (i = n; i > 0; i /= 10) {
    t = t * 10 + i % 10;
  }
  return t;
}
void eprime() {
  int i, j, k, l, m, n;
  int s;
  for (i = 1, j = 1; i <= 1000001; i++) {
    if (a[i] == 0) {
      s = rev(i);
      if (s != i)
        if (a[s] == 0) {
          c[j++] = i;
        }
    }
  }
}
int main() {
  prime();
  eprime();
  int n, m, i, j, k;
  while (scanf("%d", &n) == 1) {
    printf("%d\n", c[n]);
  }
  return 0;
}
