#include <bits/stdc++.h>
int prime[1000009];
void seive() {
  int i, j;
  prime[1] = 1;
  for (i = 2; i <= 1000; i++)
    for (j = i + i; j <= 999999; j += i) prime[j] = 1;
}
bool reverse(int n) {
  int rev, holdn, rem;
  rev = 0;
  holdn = n;
  while (n) {
    rem = n % 10;
    rev = rev * 10 + rem;
    n = n / 10;
  }
  if (holdn == rev || prime[rev])
    return false;
  else
    return true;
}
int main() {
  seive();
  int i, n, j;
  scanf("%d", &n);
  for (i = 1, j = 2; i <= n; j++)
    if (!prime[j])
      if (reverse(j)) i++;
  printf("%d\n", j - 1);
  return 0;
}
