#include <bits/stdc++.h>
using namespace std;
int m, j = 0, k = 11;
int main() {
  scanf("%d", &m);
  while (j != m) {
    k++;
    int l = k, t = 0;
    while (l > 0) {
      t *= 10;
      t += l % 10;
      l /= 10;
    }
    if (t == k) continue;
    int boo = 0;
    for (int i = 2; i <= (int)sqrt(max(k, t)); i++) {
      if ((k % i == 0) || (t % i == 0)) {
        boo = 1;
        break;
      }
    }
    if (!boo) j++;
  }
  printf("%d", k);
  return 0;
}
