#include <bits/stdc++.h>
using namespace std;
signed main() {
  set<int> pr;
  vector<int> sv(2e6 + 2), z;
  for (int i = 2; i < sv.size(); i++) {
    if (!sv[i]) {
      pr.insert(i);
      for (int j = 2 * i; j < sv.size(); j += i) {
        sv[j] = 1;
      }
    }
  }
  for (auto& it : pr) {
    string t = to_string(it);
    reverse(t.begin(), t.end());
    if (stoi(t) != it and pr.count(stoi(t))) {
      z.push_back(it);
      z.push_back(stoi(t));
    }
  }
  sort(z.begin(), z.end());
  z.resize(unique(z.begin(), z.end()) - z.begin());
  int x;
  cin >> x;
  cout << z[x - 1] << "\n";
}
