#include <bits/stdc++.h>
double const PI = 4 * atan(1.0);
using namespace std;
void OPEN() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
}
long long reverse(int x) {
  int res = 0;
  while (x > 0) {
    res = res * 10 + (x % 10);
    x /= 10;
  }
  return res;
}
bool prime[10000001];
vector<int> V;
int n;
int main() {
  memset(prime, 1, sizeof(prime));
  for (int i = 2; i <= 10000; i++)
    if (prime[i]) {
      for (int j = i * i; j <= 10000000; j += i) prime[j] = 0;
    }
  for (int i = 11;; i++) {
    if (prime[i] && prime[reverse(i)] && i != reverse(i)) {
      V.push_back(i);
      if (V.size() > 11184) break;
    }
  }
  cin >> n;
  cout << V[n - 1];
}
