#include <bits/stdc++.h>
using namespace std;
bool a[1000010];
int main() {
  memset(a, 0, sizeof(a));
  int n, ans = 0;
  cin >> n;
  int f = 2;
  for (;;) {
    if (a[f] == 0) {
      for (int i = 2 * f; i <= 1000000; i = i + f) a[i] = 1;
    }
    f++;
    if (f == 1000000) break;
  }
  for (int i = 10;; i++) {
    int i1 = i, i2 = 0;
    while (i1 > 0) {
      i2 = i2 * 10 + i1 % 10;
      i1 = i1 / 10;
    }
    if (a[i2] == 0 && a[i] == 0 && i2 != i) ans++;
    if (ans == n) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
