#include <bits/stdc++.h>
using namespace std;
long reverse(long n) {
  stringstream ss, ss1;
  ss << n;
  string s;
  ss >> s;
  reverse(s.begin(), s.end());
  ss1 << s;
  ss1 >> n;
  return n;
}
int main() {
  long n;
  cin >> n;
  long nn = 1000000;
  vector<long> lp(nn + 1);
  vector<bool> prost(nn + 1, false);
  vector<int> pr;
  for (long i = 2; i <= nn; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= nn;
         ++j)
      lp[i * pr[j]] = pr[j];
  }
  for (long i = 0; i < pr.size(); i++) prost[pr[i]] = true;
  long kol = 0;
  for (long i = 0; i < pr.size(); i++) {
    if (prost[reverse(pr[i])] == true && reverse(pr[i]) != pr[i]) kol++;
    if (kol == n) {
      cout << pr[i];
      return 0;
    }
  }
}
