#include <bits/stdc++.h>
using namespace std;
bool ispalindrom(string second) {
  for (long long i = 0; i < second.size() / 2; i++)
    if (second[i] != second[second.size() - i - 1]) return 0;
  return 1;
}
bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; }
bool isprime(long long x) {
  for (long long i = 2; i <= sqrt(x); i++)
    if (x % i == 0) return 0;
  return 1;
}
const double pi = acos(-1);
const long long mod = 998244353, MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  vector<int> v;
  for (int i = 2; i <= 1000000; i++)
    if (isprime(i)) {
      string second = to_string(i);
      reverse(second.begin(), second.end());
      int x = stoi(second);
      if (x == i) continue;
      if (isprime(x)) v.push_back(i);
    }
  int n;
  cin >> n;
  cout << v[n - 1];
  return 0;
}
