#include <bits/stdc++.h>
using namespace std;
char pr[1000000];
inline int revnum(int x) {
  int y = 0;
  for (; x; x /= 10) y = y * 10 + x % 10;
  return y;
}
int main(int argc, char *argv[]) {
  memset(pr, 1, sizeof(pr));
  pr[0] = pr[1] = 0;
  for (long long i = 2; i * i < sizeof(pr); i++)
    if (pr[i])
      for (long long j = i * i; j < sizeof(pr); j += i) pr[j] = 0;
  int n;
  cin >> n;
  for (int i = 12, c = 0; i < 1000000; i++) {
    if (pr[i] == 0) continue;
    int j = revnum(i);
    if (pr[j] && i != j) {
      c++;
      if (c == n) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
