#include <bits/stdc++.h>
using namespace std;
bool is(int b) {
  int i;
  for (i = 2; i <= sqrt(b); i++) {
    if (b % i == 0) return 0;
  }
  return 1;
}
int main() {
  long long a[20000], n = 0, b, c, i;
  for (i = 13; i < 100000000 && n < 11185; i++) {
    if (is(i)) {
      c = 0;
      b = i;
      while (b) {
        c = c * 10 + b % 10;
        b /= 10;
      }
      if (i != c && is(c)) a[++n] = i;
    }
  }
  while (cin >> n) {
    cout << a[n] << endl;
  }
}
