#include <bits/stdc++.h>
using namespace std;
const int maxint = -1u >> 1;
template <class T>
bool get_max(T& a, const T& b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
bool get_min(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
bool check(int k) {
  int z = k;
  for (int i = 2; i <= sqrt(k); i++)
    if (k % i == 0) return false;
  int c = 0;
  while (k > 0) {
    c = c * 10 + k % 10;
    k /= 10;
  }
  if (c == z) return false;
  for (int i = 2; i <= sqrt(c); i++)
    if (c % i == 0) return false;
  return true;
}
int main() {
  int d, cnt = 1, i = 14;
  cin >> d;
  while (cnt < d) {
    if (check(i)) cnt++;
    i++;
  }
  cout << i - 1 << endl;
  return 0;
}
