#include <bits/stdc++.h>
using namespace std;
inline long long IN() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void OUT(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int N = 1000000;
int Mn[N + 5], P[100000], Cnt;
char s[100];
int main() {
  for (int i = 2; i <= N; i++) {
    if (!Mn[i]) P[Mn[i] = ++Cnt] = i;
    for (int j = 1; j <= Cnt; j++) {
      if (i * P[j] > N) break;
      Mn[i * P[j]] = j;
      if (j == Mn[i]) break;
    }
  }
  int n = IN(), Nw = 0;
  for (int i = 1; i <= Cnt; i++) {
    int len = sprintf(s, "%d", P[i]), Tmp;
    reverse(s, s + len);
    sscanf(s, "%d", &Tmp);
    if (Tmp == P[i]) continue;
    int pos = lower_bound(P + 1, P + Cnt + 1, Tmp) - P;
    if (P[pos] == Tmp) {
      Nw++;
      if (Nw == n) return OUT(P[i]), 0;
    }
  }
}
