#include <bits/stdc++.h>
using namespace std;
bool b[1000001] = {0};
int n;
int main() {
  cin >> n;
  b[0] = b[1] = 1;
  for (int i = 2; i <= sqrt(1000000); i++) {
    if (b[i] == 0)
      ;
    {
      for (int j = i * i; j <= 1000000; j += i) b[j] = 1;
    }
  }
  int h = 0;
  for (int i = 13; i <= 1000000; i++) {
    if (b[i] == 0) {
      int x = i, s = 0;
      while (x) {
        s = s * 10 + x % 10;
        x /= 10;
      }
      if (s != i && b[s] == 0) h++;
      if (h == n) {
        cout << i;
        return 0;
      }
    }
  }
  return 0;
}
