#include <bits/stdc++.h>
using namespace std;
const int N = 2000005;
bool is[N];
int rev[N];
void init() {
  fill(is, is + N, 1);
  for (int i = 2; i < N; ++i) {
    if (is[i]) {
      for (int j = i + i; j < N; j += i) {
        is[j] = 0;
      }
    }
  }
  for (int i = 2; i < N; i++) {
    int j = i, t = 0;
    while (j) {
      t *= 10;
      t += j % 10;
      j /= 10;
    }
    rev[i] = t;
  }
}
int ret[N];
int main() {
  int cnt = 0;
  init();
  for (int i = 2; i < N; i++) {
    if (rev[i] >= N) continue;
    if (i == rev[i]) continue;
    if (is[i] && is[rev[i]]) {
      ret[++cnt] = i;
    }
  }
  int n;
  cin >> n;
  cout << ret[n] << endl;
  return 0;
}
