#include <bits/stdc++.h>
using namespace std;
bool p[1000001];
int n, d, r[8], t, s, to, di;
int main() {
  cin >> n;
  memset(p, 1, sizeof(p));
  p[1] = 0;
  for (int i = 2; i <= 1000; i++)
    if (p[i])
      for (int j = i * i; j <= 1000000; j += i) p[j] = 0;
  for (int i = 2; i <= 1000000; i++)
    if (p[i]) {
      d = i;
      t = 0;
      while (d) {
        r[++t] = d % 10;
        d /= 10;
      }
      s = 1;
      di = 0;
      for (int j = t; j >= 1; j--) {
        di += r[j] * s;
        s *= 10;
      }
      if (p[di] and di != i) to++;
      if (to == n) {
        cout << i;
        return 0;
      }
    }
  return 0;
}
