#include <bits/stdc++.h>
using namespace std;
bool prime[1000000 + 1];
void sieve() {
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++)
    if (prime[i])
      for (int j = i * i; j <= 1000000; j += i) prime[j] = false;
}
int rev(int n) {
  int x = 0;
  while (n) {
    x = 10 * x + (n % 10);
    n /= 10;
  }
  return x;
}
int main() {
  sieve();
  int idx;
  cin >> idx;
  int cnt = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (prime[i] && prime[rev(i)] && i != rev(i)) {
      cnt++;
      if (cnt == idx) cout << i << endl;
    }
  }
  return 0;
}
