#include <bits/stdc++.h>
using namespace std;
bool b[1100000];
int c = 0, n;
bool emp(int x) {
  int c = 0, t = x;
  while (x > 0) {
    c = c * 10 + x % 10;
    x /= 10;
  }
  if (t == c) return 1;
  return b[c];
}
int main() {
  for (int i = 2; i < 10000; i++) {
    if (!b[i]) {
      int j = i * i;
      while (j < 1000000) {
        b[j] = 1;
        j += i;
      }
    }
  }
  cin >> n;
  int k = 10;
  while (c < n) {
    if (!b[k] && !emp(k)) c++;
    k++;
  }
  cout << k - 1;
}
