#include <bits/stdc++.h>
using namespace std;
vector<int> r, e;
inline bool pal(int x) {
  r.clear();
  for (; x; x /= 10) r.push_back(x % 10);
  for (int i = (int)0; i < (int)(int)(r).size() / 2; ++i)
    if (r[i] != r[(int)(r).size() - i - 1]) return false;
  return true;
}
inline int rev(int x) {
  r.clear();
  int y = x;
  for (; x; x /= 10) r.push_back(x % 10);
  int ret = 0;
  for (int i = (int)0; i < (int)(int)(r).size(); ++i) ret = ret * 10 + r[i];
  return ret;
}
inline bool isprime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (!(x % i)) return false;
  return true;
}
inline void emirp(int x) {
  int a = 2;
  while ((int)(e).size() < x and a++) {
    if (!pal(a) and isprime(a) and isprime(rev(a))) e.push_back(a);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int a;
  cin >> a;
  emirp(a);
  cout << e[a - 1] << endl;
}
