#include <bits/stdc++.h>
using namespace std;
const int P_MAX = 1000000;
bool er[P_MAX + 1];
void sieve() {
  for (int i = 0; i < (P_MAX + 1); i++) er[i] = (i >= 2);
  for (int i = 2; i * i <= P_MAX; i++)
    if (er[i])
      for (int j = i * i; j <= P_MAX; j += i) er[j] = false;
}
int main() {
  sieve();
  int n;
  scanf("%d", &n);
  vector<int> ans;
  for (int a = 1; ans.size() < n; a++)
    if (er[a]) {
      char s[10];
      sprintf(s, "%d", a);
      int n = strlen(s);
      reverse(s, s + n);
      int p;
      sscanf(s, "%d", &p);
      if (a != p && er[p]) ans.push_back(a);
    }
  printf("%d\n", ans.back());
  return 0;
}
