#include <bits/stdc++.h>
using namespace std;
int n;
bool asal_mi(int x) {
  int t, i;
  if (!(x & 1)) return 0;
  t = sqrt(x);
  for (i = 3; i <= t; i += 2)
    if (!(x % i)) return 0;
  return 1;
}
int ters(int x) {
  int i, c = 1, t = 0, g = x;
  string s = "";
  while (g) {
    s += (g % 10) + '0';
    g /= 10;
  }
  for (i = s.size() - 1; i >= 0; i--) {
    t += c * (s[i] - '0');
    c *= 10;
  }
  return t;
}
int main() {
  int i, j = 0, t;
  cin >> n;
  for (i = 13;; i++) {
    if (asal_mi(i)) {
      t = ters(i);
      if (t == i) continue;
      if (asal_mi(t)) {
        j++;
        if (j == n) {
          cout << i << endl;
          return 0;
        }
      }
    }
  }
  return 0;
}
