#include <bits/stdc++.h>
using namespace std;
bool flag[20000000];
set<int> prime;
int cal(int a) {
  int p = 0, cnt = 0;
  for (int i = 3; i < 20000000 && cnt < a; i += 2) {
    if (!flag[i]) {
      int j = 0, k = i;
      while (k) {
        j = j * 10 + k % 10;
        k /= 10;
      }
      if (j != i && prime.count(j)) {
        ++cnt;
        p = i;
      }
    }
  }
  return p;
}
int main() {
  int s = (int)(sqrt(20000000));
  for (int i = 3; i < 20000000; i += 2) {
    if (!flag[i]) {
      prime.insert(i);
      if (i <= s) {
        int j = i * i;
        while (j < 20000000) {
          flag[j] = true;
          j += i;
        }
      }
    }
  }
  int a;
  cin >> a;
  cout << cal(a) << endl;
  return 0;
}
