#include <bits/stdc++.h>
using namespace std;
bool prime(int n) {
  int t = sqrt((double)n) + 2;
  if (n == 2) return true;
  if (n == 1) return false;
  if (n % 2 == 0) return false;
  for (int i = 3; i < t && i < n; i++)
    if (n % i == 0) return false;
  return true;
}
int rev(int n) {
  int res = 0;
  while (n > 0) {
    res = res * 10 + (n % 10);
    n /= 10;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  int curr = 0;
  for (int i = 13;; i += 2) {
    if (prime(i) && prime(rev(i)) && i != rev(i)) curr++;
    if (curr == n) {
      cout << i << endl;
      return 0;
    }
  }
}
