#include <bits/stdc++.h>
using namespace std;
int X[1000000];
int F[100000];
int main() {
  int n;
  cin >> n;
  int i, j, k, N = 1000000;
  for (i = 1; i < N; i++) X[i] = 1;
  for (i = 2; i < N; i++)
    if (X[i] == 1) {
      j = i + i;
      while (j < N) {
        X[j] = 0;
        j += i;
      }
    }
  k = 0;
  int x, y;
  for (i = 2; i < N; i++)
    if (X[i] == 1) {
      x = i;
      y = 0;
      while (x > 0) {
        y *= 10;
        y += (x % 10);
        x /= 10;
      }
      if ((X[y] == 1) && (i != y)) {
        k++;
        F[k] = i;
      }
    }
  cout << F[n] << endl;
}
