#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rnd(time(0));
template <class T>
int upmax(T &a, T b) {
  return (b > a) ? a = b, 1 : 0;
}
template <class T>
int upmin(T &a, T b) {
  return (b < a) ? a = b, 1 : 0;
}
const long long INF = 1e9;
constexpr long long mod = 1e9 + 7;
inline bool prime(int x) {
  if (x == 2) return 1;
  if (x < 2 || !(x & 1)) return 0;
  for (int i = 3; i * i <= x; i += 2)
    if (x % i == 0) return 0;
  return 1;
}
int rvs(int x) {
  int xxx = 0;
  while (x > 0) xxx = xxx * 10 + x % 10, x /= 10;
  return xxx;
}
void solve() {
  int c = 0, i = 1, p = 1, n;
  cin >> n;
  while (c < n) {
    if (prime(i))
      if (i != rvs(i) && prime(rvs(i))) c++, p = i;
    i++;
  }
  cout << p << '\n';
}
signed main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
