#include <bits/stdc++.h>
using namespace std;
int simple(int n) {
  for (int i = 2; i <= sqrt(n); i++)
    if ((n % i) == 0) return 0;
  return 1;
}
int main() {
  int m, inc = 0, tmp, p;
  string s;
  cin >> m;
  for (p = 2; inc < m; ++p) {
    if (simple(p)) {
      s = to_string(p);
      reverse(s.begin(), s.end());
      tmp = stoi(s);
      if (simple(tmp) && p != tmp) ++inc;
    }
  }
  --p;
  cout << p << endl;
  return 0;
}
