#include <bits/stdc++.h>
using namespace std;
const int X = 2000010;
int e[X], d[X], top;
void make() {
  for (int i = 2; i < X; i++)
    if (!e[i])
      for (int j = i + i; j < X; j += i) e[j] = 1;
}
int reverse(int x) {
  int sum = 0;
  while (x) {
    sum *= 10;
    sum += x % 10;
    x /= 10;
  }
  return sum;
}
int main() {
  make();
  int i, n;
  for (i = 2; i < X && top < 11200; i++) {
    n = reverse(i);
    if (!e[i] && i != n && n < X && !e[reverse(i)]) d[++top] = i;
  }
  scanf("%d", &n);
  printf("%d\n", d[n]);
  return 0;
}
