#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
using vc = vector<T>;
constexpr ll CE[] = {(ll)2e6};
ll n;
vc<bool> isP(CE[0], true);
list<ll> P;
bool OK(list<ll>::iterator it) {
  ll p = *it, res = 0;
  while (p) {
    res = res * 10 + p % 10;
    p /= 10;
  }
  return res != *it && isP[res];
}
int main() {
  scanf("%lld", &n);
  isP[0] = isP[1] = false;
  for (ll i = 0; i < CE[0]; i++) {
    if (isP[i]) {
      P.push_back(i);
    }
    for (ll k : P) {
      if (i * k >= CE[0]) break;
      isP[i * k] = false;
      if (i % k == 0) break;
    }
  }
  auto it = P.begin();
  for (ll i = 0;; i++, it++) {
    while (!OK(it)) it++;
    if (i == n - 1) break;
  }
  printf("%lld\n", *(it--));
  return 0;
}
