#include <bits/stdc++.h>
const int N = 10772160;
int np, p[N];
bool isp[N];
int main() {
  int x, y;
  memset(isp, true, sizeof(isp));
  scanf("%d", &x);
  y = np = 0;
  for (int i = 2, tmp1, v, flg; i < N; i++) {
    if (isp[i]) {
      p[np++] = i;
      if (i < (int)sqrt(N) + 1)
        for (int j = i * i; j < N; j += i) isp[j] = 0;
      v = 0;
      tmp1 = i;
      while (tmp1) {
        v = v * 10 + tmp1 % 10;
        tmp1 /= 10;
      }
      if (v != i && isp[v]) {
        flg = 1;
        for (int j = 0, ed = (int)sqrt(v) + 1; j < np && p[j] < ed; j++)
          if (v % p[j] == 0) {
            flg = 0;
            break;
          }
        if (flg) {
          y++;
          if (x == y) {
            printf("%d\n", i);
            exit(0);
          }
        }
      }
    }
  }
  return 0;
}
