#include <bits/stdc++.h>
using namespace std;
const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int intmax = 0x7fffffff;
const int mod = 1000000007;
char s1[] =
    "qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. "
    "oekh jqia yi je vydt jxu djx ucyhf.jxu ydfkj sediyiji ev q iydwbu ydjuwuh "
    "jxu edu-rqiut ydtun ev jxu ucyhf je vydt. ekjfkj q iydwbu dkcruh.";
template <class DT>
DT gcd(DT a, DT b) {
  if (b) return gcd(b, a % b);
  return a;
}
template <class DT>
void exgcd(DT a, DT b, DT &d, DT &x, DT &y) {
  if (b)
    exgcd(b, a % b, d, y, x), y -= x * (a / b);
  else
    d = a, x = 1, y = 0;
}
template <class DT>
inline DT inv(DT a, DT m) {
  DT x, y, d;
  exgcd(a, m, d, x, y);
  return (x + m) % m;
}
template <class DT1, class DT2>
inline DT2 Mod(DT1 a, DT2 mod) {
  return (a % mod + mod) % mod;
}
template <class DT, class DT1, class DT2>
inline DT mulm(DT1 a, DT2 b, DT mod) {
  DT ta(Mod(a, mod)), tb(Mod(b, mod));
  if (ta <= intmax && tb <= intmax) return 1ll * ta * tb % mod;
  DT ret(0);
  if (ta < tb) swap(ta, tb);
  for (; tb; tb >>= 1) {
    if (tb & 1) ret = (ret + ta) % mod;
    ta = (ta + ta) % mod;
  }
  return ret;
}
template <class DT>
inline DT mulm(int a, int b, DT mod) {
  return Mod(1ll * a * b, mod);
}
template <class DT, class DT1, class DT2>
inline DT expm(DT1 a, DT2 b, DT mod) {
  assert(b >= 0);
  DT ret(1), ta(Mod(a, mod));
  for (; b; b >>= 1) {
    if (b & 1) ret = mulm(ret, ta, mod);
    ta = mulm(ta, ta, mod);
  }
  return ret;
}
vector<int> prt;
vector<int> prl;
void getprime(int n) {
  prt.resize(n + 1);
  for (int i = 2; i <= n; i++) {
    if (!prt[i]) {
      prl.push_back(i);
      prt[i] = i;
    }
    for (int j = 0; prl[j] <= n / i; j++) {
      prt[i * prl[j]] = prl[j];
      if (i % prl[j] == 0) break;
    }
  }
}
template <class DT>
bool MillerRabin(DT n, int b) {
  DT p2 = (n - 1) & (1 - n);
  DT u = (n - 1) / p2;
  DT x = expm((DT)b, u, n);
  if (x == 1) return 1;
  for (DT j = 1; j < p2; j <<= 1, x = mulm(x, x, n))
    if (x == n - 1) return 1;
  return 0;
}
const int SPRPb1[4] = {2, 3, 5, 7};
const int SPRPb2[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
const vector<int> SPRPbi(SPRPb1, SPRPb1 + 4);
const vector<int> SPRPbl(SPRPb2, SPRPb2 + 7);
template <class DT>
bool isprime(DT n) {
  if (n < 2) return 0;
  if (n < prt.size()) return prt[n] == n;
  const vector<int> &base = (n <= intmax ? SPRPbi : SPRPbl);
  for (int i = 0; i < prl.size(); i++)
    if (n % prl[i] == 0) return 0;
  for (int i = 0; i < base.size(); i++) {
    if (n == base[i]) return 1;
    if (!MillerRabin(n, base[i])) return 0;
  }
  return 1;
}
template <class DT>
struct Int {
  DT n;
  Int(DT val = 0) : n(val) {}
  vector<DT> pfac;
  vector<pair<DT, int> > pfacpi;
  vector<DT> fac;
  void getpfacbyt(vector<DT> &pfac, DT n) {
    for (; n > 1; n /= prt[n]) pfac.push_back(prt[n]);
    return;
  }
  void getpfacd(DT n) {
    if (n < prt.size()) {
      getpfacbyt(pfac, n);
      return;
    }
    if (isprime(n)) {
      pfac.push_back(n);
      return;
    }
    DT d = PollardRho(n);
    getpfacd(n / d);
    getpfacd(d);
  }
  void getpfac() {
    if (abs(n) < 2) return;
    pfac.clear();
    getpfacd(abs(n));
  }
  void sortpfac() { sort(pfac.begin(), pfac.end()); }
  void getpfacpi() {
    if (!pfac.size()) return;
    int i, j, cnt;
    for (i = 0; i < pfac.size(); i = j)
      for (j = i + 1, cnt = 1; j < pfac.size(); j++)
        if (pfac[i] == pfac[j])
          cnt++;
        else {
          pfacpi.push_back(make_pair(pfac[i], cnt));
          break;
        }
    pfacpi.push_back(make_pair(pfac[j - 1], cnt));
  }
  void getfacd(int pos, DT la, DT ret) {
    if (pos == pfac.size()) {
      fac.push_back(ret);
      return;
    }
    if (la != pfac[pos]) getfacd(pos + 1, la, ret);
    getfacd(pos + 1, pfac[pos], ret * pfac[pos]);
  }
  void getfac() { getfacd(0, 0, 1); }
  void sortfac() { sort(fac.begin(), fac.end()); }
  void printpfac() {
    cout << n << '=';
    if (abs(n) < 2) {
      cout << n << endl;
      return;
    }
    if (n < 0) cout << "-1*";
    for (int i = 0; i < pfac.size(); i++)
      cout << pfac[i] << (i == pfac.size() - 1 ? '\n' : '*');
  }
  void printpfacpi() {
    cout << n << '=';
    if (abs(n) < 2) {
      cout << n << endl;
      return;
    }
    if (n < 0) cout << "-1*";
    for (int i = 0; i < pfacpi.size(); i++) {
      cout << '(' << pfacpi[i].first << '^' << pfacpi[i].second << ')'
           << (i == pfacpi.size() - 1 ? '\n' : '*');
    }
  }
  void printfac() {
    cout << "Divisor of " << n << ':';
    if (abs(n) < 2) {
      cout << n << endl;
      return;
    }
    for (int i = 0; i < fac.size(); i++)
      cout << fac[i] << (i == fac.size() - 1 ? '\n' : ' ');
  }
  string tostring() {
    DT tn = n;
    string s;
    while (tn) {
      s += tn % 10 + '0';
      tn /= 10;
    }
    for (int i = 0; i < s.size() / 2; i++) swap(s[i], s[s.size() - i - 1]);
    return s;
  }
};
int main() {
  int l = strlen(s1);
  for (int i = 0; i < l; i++) {
    s1[i] -= ('q' - 'a');
    if (s1[i] < 'a') s1[i] += 26;
  }
  getprime(1000000);
  int cnt = 0;
  int nn;
  scanf("%d", &nn);
  for (int i = 0; i < prl.size(); i++) {
    Int<int> n(prl[i]);
    string s = n.tostring();
    for (int i = 0; i < s.size() / 2; i++) swap(s[i], s[s.size() - i - 1]);
    int m;
    sscanf(s.c_str(), "%d", &m);
    if (m == n.n) continue;
    if (isprime(m)) cnt++;
    if (cnt == nn) {
      printf("%d\n", n.n);
      break;
    }
  }
}
