#include <bits/stdc++.h>
using namespace std;
bool sushu(long long n) {
  long long t = sqrt(n);
  for (long long i = 2; i <= t; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
long long ret(long long n) {
  long long ne = 0;
  while (n != 0) {
    ne = ne * 10 + n % 10;
    n = n / 10;
  }
  return ne;
}
int main() {
  long long n, ans;
  long long a[11190];
  long long i, num = 0;
  for (i = 13;; i++) {
    if (sushu(i) && sushu(ret(i)) && i != ret(i)) {
      a[num++] = i;
      if (num == 11184) break;
    }
  }
  while (cin >> n) {
    cout << a[n - 1] << endl;
  }
}
