#include <bits/stdc++.h>
using namespace std;
const int LIM = 11184;
const int MAXN = 10000000;
bool isp[MAXN];
vector<int> out;
inline int rev(int x) {
  int ret(0);
  while (x) ret = ret * 10 + x % 10, x /= 10;
  return ret;
}
void init() {
  memset(isp, true, sizeof(isp));
  isp[0] = isp[1] = false;
  for (int i = 2; i < MAXN; ++i)
    if (isp[i]) {
      for (int j = i << 1; j < MAXN; j += i) isp[j] = false;
    }
  for (int i = 2; out.size() <= LIM; ++i) {
    if (i != rev(i) && isp[i] && isp[rev(i)]) out.push_back(i);
  }
}
int main() {
  int i, j, k;
  init();
  scanf("%d", &k);
  printf("%d\n", out[k - 1]);
  return 0;
}
