#include <bits/stdc++.h>
using namespace std;
int i, j, x, t, n;
vector<int> res;
int main() {
  n = 10000000;
  vector<char> p(n + 1);
  p[0] = p[1] = 1;
  for (i = 2; i * i <= n; i++)
    if (p[i] == 0)
      for (j = i * i; j <= n; j += i) p[j] = 1;
  for (i = 13; i <= n; i++)
    if (!p[i]) {
      x = i;
      t = 0;
      while (x) {
        t = t * 10 + x % 10;
        x /= 10;
      }
      if (i != t && !p[t]) res.push_back(i);
    }
  cin >> n;
  cout << res[n - 1] << endl;
}
