#include <bits/stdc++.h>
using namespace std;
int rev(int p) {
  int rev = 0;
  while (p > 0) {
    rev *= 10;
    rev += p % 10;
    p /= 10;
  }
  return rev;
}
int main() {
  int n, coun = 0, now, a;
  bool p = true;
  vector<int> prime;
  prime.push_back(2);
  prime.push_back(3);
  prime.push_back(5);
  prime.push_back(7);
  prime.push_back(11);
  cin >> n;
  for (int i = 13; coun < n; i += 2) {
    p = true;
    for (int j = 0; prime[j] <= sqrt(i); j++) {
      if ((i) % prime[j] == 0) {
        p = false;
        break;
      }
    }
    if (p) {
      prime.push_back(i);
      if (rev(i) != i) {
        p = true;
        for (int k = 0; k < prime.size() && prime[k] <= sqrt(rev(i)); k++) {
          if (rev(i) % prime[k] == 0) {
            p = false;
            break;
          }
        }
        if (p) {
          coun++;
          now = i;
        }
      }
    }
  }
  cout << now << endl;
  return 0;
}
