#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
template <class T>
inline T sqr(T x) {
  return x * x;
}
char a[256];
const int MAXN = 1 << 20;
char prime[MAXN];
inline int rev(int x) {
  int res = 0;
  for (; x; x /= 10) res = res * 10 + x % 10;
  return res;
}
int main() {
  memset(prime, 1, sizeof prime);
  for (int i = 2; i * i < (MAXN); ++i)
    if (prime[i])
      for (int j = i * i; j < MAXN; j += i) prime[j] = false;
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 2; i < MAXN; ++i) {
    if (!prime[i]) continue;
    int x = rev(i);
    if (x == i || !prime[x]) continue;
    if (++cnt == n) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
