#include <bits/stdc++.h>
using namespace std;
bool prime[2000000];
int rev(int x) {
  vector<int> d;
  while (x) {
    d.push_back(x % 10);
    x /= 10;
  }
  reverse(d.begin(), d.end());
  int ans = 0;
  for (int i = d.size() - 1; i >= 0; i--) {
    ans *= 10;
    ans += d[i];
  }
  return ans;
}
int main() {
  memset(prime, 1, sizeof(prime));
  prime[0] = 1;
  for (int i = 2; i * i < 1000000; i++) {
    if (!prime[i]) continue;
    for (int j = i * i; j < 1000000; j += i) {
      prime[j] = 0;
    }
  }
  vector<int> ans;
  for (int i = 1; i < 1000000; i++) {
    if (!prime[i]) continue;
    int v = rev(i);
    if (v == i) continue;
    if (!prime[v]) continue;
    ans.push_back(i);
  }
  int k;
  cin >> k;
  cout << ans[k - 1];
  return 0;
}
