#include <bits/stdc++.h>
using namespace std;
int a[20000];
bool is_prime(int n) {
  if (n == 2 || n == 3) return 1;
  if (n % 6 != 1 && n % 6 != 5) return 0;
  for (int i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0) return 0;
  }
  return 1;
}
int fan(int x) {
  int e, ans(0);
  int xx = x;
  while (x) {
    e = (x % 10);
    ans = ans * 10 + e;
    x /= 10;
  }
  return ans;
}
int main() {
  int cnt(0), n;
  for (int i = 13;; i++) {
    if (is_prime(i) && is_prime(fan(i)) && (i != fan(i))) a[cnt++] = i;
    if (cnt > 12000) break;
  }
  while (cin >> n) cout << a[n - 1] << "\n";
  return 0;
}
