#include <bits/stdc++.h>
using namespace std;
bool isp[20000000 + 5];
int sol(int x) {
  vector<int> vec;
  while (x) {
    vec.push_back(x % 10);
    x /= 10;
  }
  int ans = 0;
  for (int i = 0; i < vec.size(); i++) ans = ans * 10 + vec[i];
  return ans;
}
int d;
void ss() {
  memset(isp, true, sizeof(isp));
  isp[0] = isp[1] = false;
  for (int i = 2; i * i <= 20000000; i++) {
    if (isp[i]) {
      for (int j = i * i; j <= 20000000; j += i) {
        isp[j] = false;
      }
    }
  }
  int sz = 0;
  for (int i = 2; i < 20000000; i++) {
    if (!isp[i]) continue;
    int c = sol(i);
    if (c == i) continue;
    if (isp[i] && isp[c]) {
      sz++;
      if (sz == d) {
        cout << i << endl;
        return;
      }
    }
  }
}
int main() {
  scanf("%d", &d);
  ss();
}
