#include <bits/stdc++.h>
using namespace std;
int a[1234567];
int l, js, n;
bool is_prime(int x) {
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0) return 0;
  return 1;
}
int rev(int x) {
  int ans = 0;
  while (x) {
    (ans *= 10) += x % 10;
    x /= 10;
  }
  return ans;
}
int main() {
  l = 0;
  for (int i = 0; i <= 1000000; ++i)
    if (i != rev(i) && is_prime(rev(i)) && is_prime(i)) a[++js] = i;
  while (scanf("%d", &n) != EOF) printf("%d\n", a[n]);
  return 0;
}
