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