#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  return out << p.first << ' ' << p.second;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
    out << *i << ' ';
  }
  return out;
}
template <class T, class U>
istream &operator>>(istream &in, pair<T, U> &p) {
  return in >> p.first >> p.second;
}
template <class T>
istream &operator>>(istream &in, vector<T> &v) {
  for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
    in >> (*i);
  }
  return in;
}
template <class T>
ostream &operator,(ostream &out, const T &t) {
  return out << t;
}
template <class T>
istream &operator,(istream &in, T &t) {
  return in >> t;
}
int io_init() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout, fixed, setprecision(9);
  return 1;
}
int _io_dummy = io_init();
template <class T, class U>
bool remin(T &a, const U &b) {
  return b < a ? a = b, true : false;
}
template <class T, class U>
bool remax(T &a, const U &b) {
  return b > a ? a = b, true : false;
}
const int N = 1000000;
int D[N];
void calcPrimes(int n = N) {
  for (int i = 2; i * i < n; ++i) {
    if (D[i]) {
      continue;
    }
    for (int j = i * i; j < n; j += i) {
      D[j] = i;
    }
  }
}
bool isPrime(int x) { return x > 1 && D[x] == 0; }
int rev(int x) {
  ostringstream oss;
  oss, x;
  string s = oss.str();
  reverse((s).begin(), (s).end());
  istringstream iss(s);
  iss, x;
  return x;
}
int main() {
  calcPrimes();
  string s =
      "qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. "
      "oekh jqia yi je vydt jxu djx ucyhf.\n";
  s += "jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 <= d <= 11184) - jxu "
       "edu-rqiut ydtun ev jxu ucyhf je vydt\n";
  s += "ekjfkj q iydwbu dkcruh.";
  for (int i = 0; i < (10); ++i) {
    for (typeof((s).begin()) c = ((s).begin()); c != (s).end(); ++c) {
      if (!isalpha(*c)) continue;
      if ((*c)++ == 'z') *c = 'a';
    }
  }
  cerr, s, '\n';
  int x;
  cin, x;
  int n = 0, r;
  for (int i = 13; i < N; i += 2) {
    if (isPrime(i) && (r = rev(i)) != i && isPrime(r)) {
      ++n;
      if (n == x) {
        cout, i, '\n';
        break;
      }
    }
  }
}
