#include <bits/stdc++.h>
using namespace std;
const int MAXP = 10000000;
int pri[MAXP], cnt;
bool mark[MAXP];
int setp() {
  int i, j, m;
  cnt = 1;
  pri[0] = 2;
  mark[4] = 1;
  for (i = 3; i < MAXP; i++) {
    if (!mark[i]) pri[cnt++] = i;
    for (j = 0; j < cnt, pri[j] * i < MAXP; j++) {
      mark[i * pri[j]] = 1;
      if (!(i % pri[j])) break;
    }
  }
  return cnt;
}
int res[12222];
int getr(int n) {
  int t = 0;
  while (n) {
    t = t * 10 + (n % 10);
    n /= 10;
  }
  return t;
}
int main() {
  setp();
  int r, cc = 1;
  for (int i = 0; i < cnt; i++) {
    r = getr(pri[i]);
    if (pri[i] != r && !mark[r]) res[cc++] = pri[i];
    if (cc > 11184) break;
  }
  int n;
  while (cin >> n) cout << res[n] << endl;
  return 0;
}
