#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const long long INFLL = 0x7ffffffffffffffll;
const int MOD = 1000000007;
const double eps = 1e-6;
const double PI = acos(-1.0);
const int N = 1000005;
int pnum, prime[N];
bool isprime[N];
int rpnum, rprime[N];
void pre() {
  pnum = 0;
  memset(isprime, 1, sizeof(isprime));
  for (int i = 2; i < N; i++) {
    if (isprime[i]) prime[pnum++] = i;
    for (int j = 0; j < pnum && prime[j] * i < N; j++) {
      isprime[i * prime[j]] = 0;
      if (i % prime[j] == 0) break;
    }
  }
  rpnum = 0;
  for (int i = 0; i < pnum; i++) {
    int now = prime[i];
    int rnow = 0;
    while (now) {
      rnow *= 10;
      rnow += now % 10;
      now /= 10;
    }
    now = prime[i];
    if (now != rnow && isprime[rnow]) {
      rprime[rpnum++] = now;
    }
  }
}
int main() {
  pre();
  int x;
  while (cin >> x) cout << rprime[x - 1] << endl;
}
