#include <bits/stdc++.h>
bool isPrime(int n) {
  bool ret = true;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      ret = false;
      break;
    }
  }
  return ret;
}
int revNum(int original) {
  int reversed = 0;
  while (original > 0) {
    reversed = original % 10 + reversed * 10;
    original = original / 10;
  }
  return reversed;
}
int main() {
  char str[] =
      "qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. "
      "oekh jqia yi je vydt jxu djx ucyhf. jxu ydfkj sediyiji ev q iydwbu "
      "ydjuwuh d jxu edu-rqiut ydtun ev jxu ucyhf je vydt. ekjfkj q iydwbu "
      "dkcruh";
  int len = strlen(str);
  for (int i = 0; i < len; ++i) {
    char &c = str[i];
    if ('a' <= c && c <= 'z') {
      c = c - 16;
      if (c < 'a') c = c + 26;
    }
  }
  int idx = 0;
  int n, i;
  scanf("%d", &n);
  for (i = 2; idx < n; ++i) {
    int a = i, b = revNum(i);
    if (a != b && isPrime(a) && isPrime(b)) ++idx;
  }
  printf("%d", i - 1);
  return 0;
}
