#include <bits/stdc++.h>
using namespace std;
bool prime(int n) {
  if (n <= 1) return false;
  for (int i = 2; i * i <= n; i++)
    if (!(n % i)) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  register int i = 0, n, a, k = 0;
  stringstream ss;
  cin >> n;
  while (1) {
    i++;
    if (prime(i)) {
      stringstream ss;
      ss << i;
      string str;
      ss >> str;
      string test = "";
      for (int j = str.length() - 1; j > -1; j--) test += str[j];
      int gg = atoi(test.c_str());
      if (prime(gg) && str != test) k++;
      if (k == n) {
        cout << i << endl;
        break;
      }
    }
  }
}
