#include <bits/stdc++.h>
int Fl, Pn, SI = 100;
char mch = ' ', ch, Bf[21];
template <typename t>
void in(t& a) {
  a = 0;
  ch = getchar();
  Fl = 1;
  while ((ch < '0') || (ch > '9')) Fl = (ch == '-') ? -Fl : Fl, ch = getchar();
  while ((ch >= '0') && (ch <= '9')) a = a * 10 + ch - '0', ch = getchar();
  a *= Fl;
}
template <typename t>
void out(t a) {
  if (a < 0) putchar('-'), a = -a;
  if (a == 0) putchar('0');
  while (a) Bf[++Pn] = a % 10 + '0', a /= 10;
  while (Pn) putchar(Bf[Pn]), --Pn;
  putchar(mch);
}
template <typename t, typename... ARGS>
void in(t& a, ARGS&... args) {
  in(a);
  in(args...);
}
template <typename t, typename... ARGS>
void out(t a, ARGS... args) {
  out(a);
  out(args...);
}
using namespace std;
int n, T = 9, ptp;
bool p[1000001] = {1, 1};
bool pd(int i) {
  if (p[i]) return 0;
  int s = 0, t = i, a[20] = {}, l = 0;
  while (t) a[++l] = t % 10, t /= 10;
  for (register int i = 1; i <= l; i++) s = s * 10 + a[i];
  if (s == i) return 0;
  return !p[s];
}
int main() {
  in(n);
  for (register int i = 2; i <= 1e6; i++)
    if (!p[i]) {
      for (register int j = 2; j <= 1e6 / i; j++) p[i * j] = 1;
    }
  for (register int cnt = 1; cnt <= n; cnt++)
    while (!pd(++T))
      ;
  cout << T;
  return 0;
}
