#include <bits/stdc++.h>
using namespace std;
int v[1000000], p[1000000];
int ch(int x) {
  char s[10];
  sprintf(s, "%d", x);
  int l = strlen(s);
  reverse(s, s + l);
  sscanf(s, "%d", &x);
  return x;
}
int is(int x) {
  if (x == 2) return 1;
  if (x % 2 == 0) return 0;
  for (int i = 3; i * i <= x; i++)
    if (x % i == 0) return 0;
  return 1;
}
int main() {
  memset(v, 0, sizeof(v));
  int t = 0;
  for (int i = 2; i < 1000000; i++)
    if (!v[i]) {
      int tp = ch(i);
      if (tp != i && is(tp)) p[++t] = i;
      for (int j = 1; j * i < 1000000; j++) v[j * i] = 1;
    }
  int a;
  while (cin >> a) cout << p[a] << endl;
}
