#include <bits/stdc++.h>
using namespace std;
int is_prime[1111111];
int is_ok(int n) {
  int revn = 0;
  int temp = n;
  if (is_prime[n] == 0) return 0;
  while (n > 0) {
    revn = revn * 10 + n % 10;
    n /= 10;
  }
  if (is_prime[revn] == 1 && revn != temp) return 1;
  return 0;
}
int main() {
  int a, i, count = 1, j;
  for (i = 2; i < 1111111; i++) {
    is_prime[i] = 1;
  }
  for (i = 2; i < 1111111; i++) {
    if (is_prime[i])
      for (j = i * 2; j < 1111111; j += i) {
        is_prime[j] = 0;
      }
  }
  i = 13;
  cin >> a;
  while (count < a) {
    i++;
    count += is_ok(i);
  }
  cout << i << endl;
  return 0;
}
